2015-01-16 49 views
-1

我尝试从代码添加按钮并向其中添加触发器和设置器。我wolud喜欢创建这样的按钮:wpf - 从代码添加setter

<Button Height="25" Width="100" Name="TestColorButton" Margin="10, 5, 0, 0"> 
    <TextBlock Text="{Binding Text, ElementName=ColorTextBox}"></TextBlock> 
     <Button.Style> 
      <Style TargetType="{x:Type Button}"> 
       <Setter Property="Background" Value="{Binding Fill, ElementName=NormalRectangle}"/> 
       <Setter Property="Template"> <!-- I need this setter --> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Border Background="{TemplateBinding Background}"> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
                </Border> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{Binding Fill, ElementName=MouseOverRectangle}"/> 
     </Trigger> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter Property="Background" Value="{Binding Fill, ElementName=ClickRectangle}"></Setter> 
     </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Button.Style> 
    </Button> 

我发现如何做除一个setter之外的所有东西。我通过评论检查了它。我尝试了很多次,我只有这样的东西:

ContentPresenter contentPresenter = new ContentPresenter 
     { 
      HorizontalAlignment = HorizontalAlignment.Center, 
      VerticalAlignment = VerticalAlignment.Center 
     }; 

     Border border = new Border(); 
     var binding = new Binding("Background"); 
     binding.RelativeSource = new RelativeSource(RelativeSourceMode.Self); 
     BindingOperations.SetBinding(border, BackgroundProperty, binding); 
     border.Child = contentPresenter; 

     ControlTemplate controlTemplate = new ControlTemplate(typeof (Button)); 
     Setter templateSetter = new Setter(TemplateProperty, controlTemplate); 

     style.Setters.Add(templateSetter); 

我知道它不能工作,但我不知道如何做不同。

+0

@Franck对不起,它是。在WPF中的程序代码中创建UI元素不仅是不好的做法,而且很繁琐。 OP已经拥有了所需的XAML,他只需要放下程序心态并理解WPF的心态。我不确定我的评论如何“不具有建设性”。 –

+0

这听起来很积极和迫切,以取消他所做的一切。但是你是对的,因为它应该保持XAML,但有时你没有选择,比如当你的界面完全可定制并保存在数据库中,并保存为XAML代码片段时,如果在其他语言中使用相同的属性,这些代码不起作用。 – Franck

+0

我知道这不是一个好习惯,但有没有另一种方式允许用户从GUI添加按钮?我不知道用户将添加多少个按钮。 – Eleid

回答

0

模板可以通过代码轻松添加。这里有一个标签的例子。

// create the label with some context 
var lbl = new Label() { DataContext = new ImageData(imagePath) }; 

// control template i want to put on the label 
ControlTemplate labelLayout = new ControlTemplate(); 

// will be my main container that will be by template later on 
FrameworkElementFactory grdContainer = new FrameworkElementFactory(typeof(Grid)); 
grdContainer.Name = "myContainer"; 

// another element but ill put it in the grid (template) 
FrameworkElementFactory myImage = new FrameworkElementFactory(typeof(Image)); 

// some bindings and setters 
myImage.SetBinding(Image.SourceProperty, new Binding("ImagePath")); 
myImage.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Center); 
myImage.SetValue(Image.HeightProperty, height); 
myImage.SetValue(Image.WidthProperty, double.NaN); 
myImage.SetValue(Image.SnapsToDevicePixelsProperty, true); 

// add the image to the grid 
grdContainer.AppendChild(myImage); 

// set the visual layout of the template to be the grid (main container) 
labelLayout.VisualTree = grdContainer; 

// set the template (line you are looking for mostly) 
lbl.Template = labelLayout; 
+0

我担心,我是wpf中的新成员,我不知道如何翻译它以满足我的需求。当我尝试它时,其他制定者停止了他们的工作,并且按钮看起来像普通按钮,而不是以我的触发器的样式。我用'新的Setter(property,value)'创建其他setter,然后将其添加到样式中,然后将样式添加到我的按钮中。 – Eleid

+0

上面的代码似乎让你更容易理解来自winforms环境,它的工作原理,但在XAML中处理比在代码后面处理更容易。 – Franck

+0

感谢您的帮助!我做的! – Eleid