如何动态地改变按钮模板WPF

如何动态地改变按钮模板WPF

本文介绍了如何动态地改变按钮模板WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何更改按钮模板动态?

我有一个组合框其中,通过改变自己的选择的价值我想换一个按钮 模板
 这是我一直在努力做的事情:

 < Window.Resources>
    <控件模板X:键=ButtonControlTemplate1的TargetType ={X:类型按钮}>
        <网格和GT;
            <矩形填充=#FF2D2D7A保证金=7.5,9.5,8.5,11行程=黑
                       半径X =45半径=45StrokeThickness =6/>
        < /网格和GT;
    < /控件模板>
    <控件模板X:键=ButtonControlTemplate2的TargetType ={X:类型按钮}>
        <网格和GT;
            < ED:正多边形填写=#FFE7F9C9HEIGHT =自动InnerRadius =0.47211
                               保证金=20.5,16,15.5,8PointCount =5拉伸=填充
                               行程=黑StrokeThickness =6WIDTH =自动/>
        < /网格和GT;
    < /控件模板>
< /Window.Resources><电网X:NAME =LayoutRoot>
    <组合框名称=GroupBoxHeaderComboBox的ItemsSource ={绑定路径=集合}
              的DisplayMemberPath =密钥HEIGHT =52保证金=211.5,60,230.5,0
              VerticalAlignment =顶级的SelectedIndex =1/>
    <按钮内容=按钮的Horizo​​ntalAlignment =左HEIGHT =102保证金=47.5,0,0,91
            VerticalAlignment =底WIDTH =132
            模板={DynamicResource ButtonControlTemplate2}/>
    <按钮内容=按钮的Horizo​​ntalAlignment =右HEIGHT =112.5保证金=0,0,27.5,85
            VerticalAlignment =底WIDTH =153
            模板={DynamicResource ButtonControlTemplate1}/>
    <按钮内容=按钮HEIGHT =102保证金=239.5,0,252.5,13.5
            VerticalAlignment =底
            模板={绑定的ElementName = GroupBoxHeaderComboBox,路径= SelectedItem.Value}/>
< /网格和GT;

和这里有关联的模板取值:

 < Window.Resources>
    <控件模板X:键=ButtonControlTemplate1的TargetType ={X:类型按钮}>
        <网格和GT;
            <矩形填充=#FF2D2D7A保证金=7.5,9.5,8.5,11行程=黑
                       半径X =45半径=45StrokeThickness =6/>
        < /网格和GT;
    < /控件模板>
    <控件模板X:键=ButtonControlTemplate2的TargetType ={X:类型按钮}>
        <网格和GT;
            < ED:正多边形填写=#FFE7F9C9HEIGHT =自动InnerRadius =0.47211
                               保证金=20.5,16,15.5,8PointCount =5拉伸=填充
                               行程=黑StrokeThickness =6WIDTH =自动/>
        < /网格和GT;
    < /控件模板>
< /Window.Resources>

和背后的code:

 公共部分类主窗口:窗口
{
    公共字典<字符串,字符串>采集
    {
        得到;
        私人集;
    }    公共主窗口()
    {
        this.InitializeComponent();
        的DataContext =这一点;
        收集=新词典<字符串,字符串>()
        {
            {DynamicResource ButtonControlTemplate2,{DynamicResource ButtonControlTemplate2}},
            {DynamicResource ButtonControlTemplate1,{DynamicResource ButtonControlTemplate2}},        };
    需要下面这点创建对象//将code。
    }
}

有没有acomplish这另一种方式genric?......我想,大部分的code将是XAML。

编辑:

有一个点使用样式办呢?比方说,我想要更多然后一个对象采取行动,否则就是有一点改变风格,并从那里做这一切?


解决方案

 公共部分类主窗口:窗口
    {
        公共主窗口()
        {
            的InitializeComponent();            的DataContext =这一点;
        }        公共字典<字符串的ControlTemplate>采集
        {
            得到
            {
                字典<字符串的ControlTemplate> CONTROLTEMPLATES =新词典<字符串的ControlTemplate>();
                controlTemplates.Add(ButtonControlTemplate1,FindResource(ButtonControlTemplate1)作为的Con​​trolTemplate);
                controlTemplates.Add(ButtonControlTemplate2,FindResource(ButtonControlTemplate2)作为的Con​​trolTemplate);
                返回CONTROLTEMPLATES;
            }
        }
    }

How can I change a Button template dynamically?

I have a ComboBox where by changing his selected value I want to change a Button Template. This is what I have been trying to do:

<Window.Resources>
    <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
        <Grid>
            <Rectangle Fill="#FF2D2D7A" Margin="7.5,9.5,8.5,11" Stroke="Black"
                       RadiusX="45" RadiusY="45" StrokeThickness="6"/>
        </Grid>
    </ControlTemplate>
    <ControlTemplate x:Key="ButtonControlTemplate2" TargetType="{x:Type Button}">
        <Grid>
            <ed:RegularPolygon Fill="#FFE7F9C9" Height="Auto" InnerRadius="0.47211"
                               Margin="20.5,16,15.5,8" PointCount="5" Stretch="Fill"
                               Stroke="Black" StrokeThickness="6" Width="Auto"/>
        </Grid>
    </ControlTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <ComboBox Name="GroupBoxHeaderComboBox" ItemsSource="{Binding Path=collection}"
              DisplayMemberPath="Key" Height="52" Margin="211.5,60,230.5,0"
              VerticalAlignment="Top" SelectedIndex="1"/>
    <Button Content="Button" HorizontalAlignment="Left" Height="102" Margin="47.5,0,0,91"
            VerticalAlignment="Bottom" Width="132"
            Template="{DynamicResource ButtonControlTemplate2}"/>
    <Button Content="Button" HorizontalAlignment="Right" Height="112.5" Margin="0,0,27.5,85"
            VerticalAlignment="Bottom" Width="153"
            Template="{DynamicResource ButtonControlTemplate1}"/>
    <Button Content="Button" Height="102" Margin="239.5,0,252.5,13.5"
            VerticalAlignment="Bottom"
            Template="{Binding ElementName=GroupBoxHeaderComboBox, Path=SelectedItem.Value}"/>
</Grid>

And here are the associated Templates:

<Window.Resources>
    <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
        <Grid>
            <Rectangle Fill="#FF2D2D7A" Margin="7.5,9.5,8.5,11" Stroke="Black"
                       RadiusX="45" RadiusY="45" StrokeThickness="6"/>
        </Grid>
    </ControlTemplate>
    <ControlTemplate x:Key="ButtonControlTemplate2" TargetType="{x:Type Button}">
        <Grid>
            <ed:RegularPolygon Fill="#FFE7F9C9" Height="Auto" InnerRadius="0.47211"
                               Margin="20.5,16,15.5,8" PointCount="5" Stretch="Fill"
                               Stroke="Black" StrokeThickness="6" Width="Auto"/>
        </Grid>
    </ControlTemplate>
</Window.Resources>

And the code behind:

public partial class MainWindow : Window
{
    public Dictionary<string, string> collection
    {
        get;
        private set;
    }

    public MainWindow()
    {
        this.InitializeComponent();
        DataContext = this;
        collection = new Dictionary<string, string>()
        {
            { "DynamicResource ButtonControlTemplate2", "{DynamicResource ButtonControlTemplate2}"},
            { "DynamicResource ButtonControlTemplate1", "{DynamicResource ButtonControlTemplate2}"},

        };
    // Insert code required on object creation below this point.
    }
}

Is there another genric way to acomplish this?... I want that most of the code would be xaml.

EDIT:

Is there a point to do it using a style? Let's say I want more then one object to act, otherwise is there a point to change the style and to do it all from there?

解决方案
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public Dictionary<string, ControlTemplate> collection
        {
            get
            {
                Dictionary<string, ControlTemplate> controlTemplates = new Dictionary<string, ControlTemplate>();
                controlTemplates.Add("ButtonControlTemplate1", FindResource("ButtonControlTemplate1") as ControlTemplate);
                controlTemplates.Add("ButtonControlTemplate2", FindResource("ButtonControlTemplate2") as ControlTemplate);
                return controlTemplates;
            }
        }
    }

这篇关于如何动态地改变按钮模板WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:47