[英]WPF Binding Properties of a new Button Template
Ok i have made a new style for the Buttons
that i want to use in my app. 好的,我已经为要在应用程序中使用的
Buttons
制作了新样式。 The general idea is to have 10 square Button
in which i will have an Image
and under this a TextBlock
. 一般的想法是有一个10方形的
Button
在其中我将拥有一个Image
并在此之下有一个TextBlock
。 My problem is that i want to set my Image.Source
and my TextBlock.Text
different for each Button
. 我的问题是我想为每个
Button
设置不同的Image.Source
和TextBlock.Text
。 What im doing is this : 我在做什么是这样的:
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="5" BorderThickness="1" Background="Gray" BorderBrush="Orange">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Margin="0,5,0,0" Grid.Row="0" Width="25" Height="25" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock FontSize="12"/>
</Grid>
</Border>
What should i do in order to make use of my style like this? 我该怎么做才能利用我的这种风格?
<Button Style="{DynamicResource MenuButtons}" Text="????" x:Name="LiveB" Source="????" Click="Live_Click"/>
Thanks in advance. 提前致谢。
If all you need is an image and some text, you can make it work (mis)using the button's .Tag
and .Content
: 如果你需要的是一张图片和一些文字,就可以使用该按钮使其工作(MIS)
.Tag
和.Content
:
<Style x:Key="MenuButtons" ...
...
<ControlTemplate TargetType="{x:Type Button}" >
<Border x:Name="Border" CornerRadius="5" BorderThickness="1" Background="Gray" BorderBrush="Orange">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Width="25" Height="25" VerticalAlignment="Center" HorizontalAlignment="Center"
Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" />
<TextBlock FontSize="12" Text="{TemplateBinding Content}" Grid.Row="1" />
</Grid>
</Border>
</ControlTemplate>
...
..and then: ..接着:
<Button Style="{DynamicResource MenuButtons}" x:Name="LiveB"
Content="MyText la la la..." Tag="http://programming.enthuses.me/1.png"
Click="Live_Click" />
If you need more custom content, you need to look at Konstantin Zhukov's comment and Attached Properties
. 如果您需要更多自定义内容,则需要查看Konstantin Zhukov的评论和
Attached Properties
。
(Why you can't just use "{TemplateBinding Tag}"
instead of "...{RelativeSource TemplatedParent}..."
? I don't know, but TemplateBinding
is generally a really fragile shortcut that doesn't always do what it's supposed to do...) (为什么您不能只使用
"{TemplateBinding Tag}"
而不是"...{RelativeSource TemplatedParent}..."
?我不知道,但是TemplateBinding
通常是一个非常脆弱的快捷方式,并不总是会做什么它应该做的...)
What you need to do is create aa class which will act as DataContext to button 您需要做的是创建一个将用作DataContext按钮的类
Class ButtonDataContext
{
public String TextBlockText {get;set;}
public String ImageSource {get;set;}
}
then do 然后做
LiveB.DataContext = new ButtonDataContext(){TextBlockText = "text", ImageSource = "Image Path"};
in control template XAML 在控制模板XAML中
<Image Source="{Binding Path=ImageSource}" Margin="0,5,0,0" Grid.Row="0" Width="25" Height="25" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding Path=TextBlockText}" FontSize="12"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.