2015-01-16 48 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