重复使用多个元素的模板 [英] Reuse Template for Multiple Elements
问题描述
我正在应用程序中创建按钮样式,但是我注意到,对于每个按钮,我必须一次又一次地重复相同的步骤,我正在以下模板中寻找一种方法,以便可以传递特定的属性每个路径元素或路径的数量 按钮不同,但其他所有元素都相同且顺序相同,我在这里尝试搜索但找不到任何对我有帮助的特定示例,这是xaml:
I am creating button styles in my application, but i noticed that for every button i have to repeat the same steps again and again, i am looking for a way in the following template so that i can pass particular properties of Path element or Path for every button different but all the other elements would be same and in same order, i have tried here searching but not able to find any particular example that was helpful to me, here is the xaml :
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
<Grid x:Name="MainGrid">
<Rectangle
x:Name="MainRectangle"
Fill="#00000000"
RadiusX="5"
RadiusY="5"/>
<ContentPresenter
x:Name="Presenter"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextBlock.Foreground="#BB225588"/>
<Path Name="Minimize" Data="M0,0L32,0 32,8.6899995 0,8.6899995z" Stretch="Fill" Fill="White" StrokeThickness="2" Width="17" Height="5" VerticalAlignment="Center" HorizontalAlignment="Center"
Stroke="White" >
</Path>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="2" Color="White" BlurRadius="20"></DropShadowEffect>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainRectangle" Property="Fill" Value="{StaticResource ClickEffect}"/>
<Setter TargetName="Minimize" Property="Stroke" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
现在在上面的模板中,我想要的是某种方式来与每个按钮绑定Path元素属性,或者以某种方式为每个按钮注入不同的Path元素,其余的将保持不变,所以我不想再次重复相同的样式然后再次.我怎么
可以实现的.
Now in the above template what i want is some way to bind with every button the Path element properties or some way to inject the Path element different for every button, rest will remain same, so i don't want to repeat the same style again and again. How i
can achieve it.
[如果帖子有助于解决您的问题,请点击 标记为答案"; 或单击 投票为有帮助"; 该帖子的按钮.通过将帖子标记为已回答"或有帮助",您可以帮助其他人更快地找到答案. ]
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
推荐答案
您好Ehsan Sajjad,
Hi Ehsan Sajjad,
您也许可以使用附件属性,可以通过绑定名称设置控件的模板.
You may be able to use the attachment properties, you can set the control's template by the binding name.
<Button x:Name="butons" local:ButtonTemplatesStyles.ButtonTemplates="{Binding ButtonTemplate}" Margin="30,30,151,207">Hello</Button>
public class ButtonTemplatesStyles : DependencyObject
{
public static string GetButtonTemplates(DependencyObject obj)
{
return (string)obj.GetValue(ButtonTemplatesProperty);
}
public static void SetButtonTemplates(DependencyObject obj, double value)
{
obj.SetValue(ButtonTemplatesProperty, value);
}
public static readonly DependencyProperty ButtonTemplatesProperty =
DependencyProperty.RegisterAttached(
"ButtonTemplates",
typeof(string),
typeof(ButtonTemplatesStyles),
new PropertyMetadata("", FinalValueChanged));
private static void FinalValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
string newvalue = (string)e.NewValue;
Button btn = obj as Button;
if (newvalue !="")
{
ControlTemplate btControlTemplate = (ControlTemplate)Application.Current.FindResource(newvalue);
btn.Template = btControlTemplate;
}
}
}
最好的问候,
吕汉楠
这篇关于重复使用多个元素的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!