2017-08-27 78 views
-2

我之前爲自定義控件創建了一些自定義綁定,但由於此例是針對按鈕的window.resources樣式的,(而非控件模板),我不知道後面的代碼從哪裏開始。我將在哪裏創建視圖模型,以及它會繼承或引用它?爲window.resources風格創建自定義綁定屬性

XAML:

<Style x:Key="UnifiedButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="Margin" Value="{Binding Margin}"/> 

     <Setter Property="Background" Value="#FFDDDDDD"/> 
     <Setter Property="BorderBrush" Value="#FF707070"/> 
     <Setter Property="Foreground" Value="#FF000000"/> 

     <Setter Property="BorderThickness" Value="1"/> 

     <Setter Property="Content" Value="Button"/> 

     <Setter Property="Width" Value="75"/> 
     <Setter Property="Height" Value="20"/> 
     <Setter Property="Padding" Value="5"/> 

     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 

     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid 
         x:Name="ButtonGrid" 
         Background="{TemplateBinding Background}" 
         OpacityMask="{TemplateBinding OpacityMask}"> 
         <Border 
          x:Name="ButtonBorder" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          OpacityMask="{TemplateBinding OpacityMask}" 

          BorderThickness="{TemplateBinding BorderThickness}"> 

          <Label 
           x:Name="ButtonLabel" 
           Foreground="{TemplateBinding Foreground}" 

           Padding="{TemplateBinding Padding}"> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          </Label> 
         </Border> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="ButtonGrid" Property="Background" Value="{Binding HoverColorBackground}"/> 
          <Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{Binding HoverColorBorder}"/> 
          <Setter TargetName="ButtonLabel" Property="Foreground" Value="{Binding HoverColorForeground}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

Hovercolor制定者在這裏

回答

2

關鍵創建一個視圖模型定製WPF按鈕的顏色是一種錯誤的做法。按鈕顏色方案是嚴格屬於視圖的東西。還有許多按鈕意味着許多視圖模型實例,因爲每個按鈕可能都希望是唯一的 - 對於如此簡單的設置來說,代碼太多。

Button類沒有足夠的依賴項屬性來設置表示HoverColorBackground/HoverColorBorder/HoverColorForeground的顏色。替代方法是創建一個派生的Button類(當DP是一些複雜類型和/或具有關聯的邏輯時,要走的路)或使用附加屬性。我寫過a tip, which coveres the second approach

短版

創建一個連接DP

public static class Alt 
{ 
    #region Background 
    public static readonly DependencyProperty BackgroundProperty = 
       DependencyProperty.RegisterAttached("Background", typeof(Brush), 
       typeof(Alt), new PropertyMetadata(null)); 

    public static Brush GetBackground(DependencyObject obj) 
    { 
     return (Brush)obj.GetValue(Alt.BackgroundProperty); 
    } 

    public static void SetBackground(DependencyObject obj, Brush value) 
    { 
     obj.SetValue(Alt.BackgroundProperty, value); 
    } 
    #endregion 
} 

組自定義值,物業

<Button Content="Blue" Foreground="White" Margin="5" 
     Background="Blue" ui:Alt.Background="DarkBlue"/> 

確保模板知道如何使用該屬性

<Trigger Property="IsMouseOver" Value="True"> 
    <Setter TargetName="ButtonGrid" 
      Property="Background" 
      Value="{Binding Path=(ui:Alt.Background), 
          RelativeSource={RelativeSource TemplatedParent}}"/> 
</Trigger> 

任何控制工程。許多DP可以以任意組合混合。

+0

我試過你的方法,但是每當我在datacontext中設置其中一個屬性時,就會拋出一個stackoverflowexception。我沒有絲毫的線索爲什麼。當屬性沒有設置,但只是簡單的默認datacontext,它不會拋出錯誤,我沒有看到任何遞歸循環 – Varscott11

+0

編輯:Nevermind上述評論。我忘了給主類添加一個靜態導致錯誤,你猜它是個例外。像這個答案很多。 – Varscott11