bgreber

hi all,

i have to following problem:

in my application i use buttons in a toolbar, which are defined as following:

<Button>
<StackPanel>
<Image Source="image.png"/>
<TextBlock Text="ButtonText"/>
<StackPanel>
</Button>

the problem now is that in some cases, i need to change the image and the text for those buttons.
for this to work, in my case, i need to give the image and the textblock of each button a Name, right

is there a way to make a controltemplate, over which i can directly access the image (source) and text properties like
<Button Text/Content="ButtonText" Image/ImageSource="image.png"/>


thank you for any help Smile


Re: Windows Presentation Foundation (WPF) need help with controltemplate

lee d

this post should help

http://blogs.msdn.com/mikehillberg/archive/2007/02/01/ParameterizedTemplates.aspx

when do you want to change the text/image






Re: Windows Presentation Foundation (WPF) need help with controltemplate

bgreber

yeah lee, that was exactly what i was looking for, thank you Smile thank you
the only problem left is that if i put this button into a toolbar, my style is overridden from the toolbar button style .. how can i avoid this

btw: to your question, i want to change this properties during runtime in codebehind (programmatically)




Re: Windows Presentation Foundation (WPF) need help with controltemplate

lee d

you could do something like this, if you have only text

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=681467&SiteID=1






Re: Windows Presentation Foundation (WPF) need help with controltemplate

bgreber

what do you mean with 'only text' i need text and the image on my button (in the toolbar) Smile
i tried this:
Code Snippet


<ToolBar Margin="35,72,58,134">
<ToolBar.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
<Style TargetType="{x:Type Button}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
<StackPanel>
<Image Source="{Binding Tag,
RelativeSource={RelativeSource
FindAncestor,
AncestorType='Button'}}" Height="24" Width="24" Margin="0,0,0,4"/>
<TextBlock HorizontalAlignment="Center"
Text="{Binding Content,
RelativeSource={RelativeSource
FindAncestor,
AncestorType='Button'}}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

</ToolBar.Resources
>

<Button Tag="c:\1.bmp" Content="Test"/>

</ToolBar>


the controltemplate works for the button outside the toolbar, but now, with the button inside the toolbar, i get an exception :/
is this because of the two style tags in toolbar.resources (one with my control template, the other with the basedon toolbarbutton...)




Re: Windows Presentation Foundation (WPF) need help with controltemplate

lee d

try putting the buttons in DockPanel

Code Snippet

<ToolBar >

<DockPanel>

<Button Tag="c:\users\public\pictures\sample pictures\Garden.jpg" Content="Flowers" />

<Button Tag="c:\users\public\pictures\sample pictures\Tree.jpg" Content="Trees" />

<Button Tag="c:\users\public\pictures\sample pictures\Toco Toucan.jpg" Content="Toucans" />

</DockPanel>

</ToolBar>






Re: Windows Presentation Foundation (WPF) need help with controltemplate

bgreber

well i tried that alredy but that way i loose the toolbar button style too.
isnt it possible to have the toolbar-button-style on my custom templated image-buttons too




Re: Windows Presentation Foundation (WPF) need help with controltemplate

lee d

setting the ContentTemplate of the Button seems to work fine






Re: Windows Presentation Foundation (WPF) need help with controltemplate

bgreber

thanks for your help lee Smile
finally i got the thing to work using a x:Key in the Style