2010-10-05 61 views
0

如何动态更改Button模板?如何动态更改按钮模板WPF

我有一个ComboBox通过改变他的选择值,我想改变一个ButtonTemplate。 这是我一直在努力做的事情:

<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> 

这里是相关Template S:

<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> 

而后面的代码:

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. 
    } 
} 

是还有另一种genric的方式来实现这一点?...我希望大部分代码w应该是xaml。

编辑:

是否有一个点使用样式办呢?比方说,我希望有更多的对象来采取行动,否则是否有一点需要改变风格,并从那里做到这一点?

回答

2
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; 
      } 
     } 
    } 
+0

非常感谢。 – 2010-10-05 13:56:09

+0

我似乎有一个问题,我把FindResource放在虚拟机里面,我没有对HOM的任何帮助,你可以帮忙吗? – 2010-10-05 14:25:33

+0

控制模板和其他资源属于视图,如果遵循MVVM,您的虚拟机不应引用您的视图。我会将键存储在VM(“ButtonControlTemplate1”,“ButtonControlTemplate2”)中,并在视图类中使用一些代码将键映射到使用FindResource的ControlTemplatwe。如果要将ControlTemplates存储在VM中,则可以使用后面的代码在VM中创建它们,或者使视图在其Loaded处理程序中的VM中填充ControlTemplate集合。 – 2010-10-05 19:34:32

1

您可以使用数据触发器并在xaml中完成所有操作。

它使用一棵树,但概念是相同的

<Window x:Class="WpfBindingTest.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WpfBindingTest" 
Title="Window3" Height="300" Width="300" Name="win3" > 
<Window.Resources> 
    <XmlDataProvider x:Key="treeData" XPath="*"> 
     <x:XData> 
      <Items Name="Items" xmlns=""> 
       <Item1/> 
       <Item2> 
        <Item22/> 
        <Item12/> 
        <Item13> 
         <Item131/> 
         <Item131/> 
        </Item13> 
       </Item2> 
      </Items> 
     </x:XData> 
    </XmlDataProvider> 
    <HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}" 

X:键为 “模板”>

here

(我只是用Google搜索获得一个例子更快)

0

在Windows中创建资源的ControlTemplate,

<Window.Resources> 
    <ControlTemplate x:Key="GreenTemplate" TargetType="{x:Type Button}"> 
     <Grid> 
      <Ellipse Fill="Green"/> 
      <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
     </Grid> 
    </ControlTemplate> 
</Window.Resources> 

现在,在运行时可以更改按钮的模板属性。

private void Button_Clicked(object sender, RoutedEventArgs e) 
    { 
     Button btn = e.OriginalSource as Button; 
     if (btn != null) 
     { 
      btn.Template = FindResource("GreenTemplate") as ControlTemplate; 
     } 
    }