WPF:元素的个人风格

时间:2011-02-28 17:24:06

标签: .net wpf styles

C#

public class MyButton : Button
{
    public Color MyColor { get; set; }
}

WPF:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication3">
    <Window.Resources>
        <Style TargetType="my:MyButton">
            <Setter Property="Background" Value="SkyBlue"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <CheckBox Content="Individual Mode" x:Name="ModeCheckBox" Grid.Row="0" />
        <my:MyButton Content="B1" x:Name="myB1" Grid.Row="1" MyColor="Aqua"/>
        <my:MyButton Content="B2" x:Name="myB2" Grid.Row="2"/>
        <my:MyButton Content="B3" x:Name="myB3" Grid.Row="3" MyColor="Green"/>
    </Grid>
</Window>

现在。 我希望我的所有按钮都是蓝色(这是普通模式),每个按钮都有特定的“MyColor”(这是个人模式)。

有可能吗?

一旦我尝试按代码设置样式元素(在我的情况下为Background),我就破坏了样式逻辑并且它不再切换回来。

2 个答案:

答案 0 :(得分:2)

您需要为MyButton控件定义默认样式;它将存储在Themes/Generic.xaml文件中。

    <Style TargetType="{x:Type local:MyButton}" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="MyColor" Value="Blue"/>
    </Style>

默认情况下,该按钮将MyColor属性设置为Blue,除非在每个控件实例上另有明确指定。

    <Grid>
        <local:MyButton Height="30" VerticalAlignment="Top">MyButtonDefault</local:MyButton>
        <local:MyButton Height="30" VerticalAlignment="Bottom" MyColor="Red">MyButtonRed</local:MyButton>
    </Grid>

以下是MyButton来源,我根据Background属性更改此示例的MyColor属性。

    public class MyButton : Button
    {
        static MyButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
        }

        public Color MyColor
        {
            get { return (Color)GetValue(MyColorProperty); }
            set { SetValue(MyColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyColorProperty =
            DependencyProperty.Register("MyColor", typeof(Color), typeof(MyButton), new PropertyMetadata(new PropertyChangedCallback(Change)));

         private static void Change(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            MyButton element = target as MyButton;
            element.Background = new SolidColorBrush((Color)e.NewValue);
        }
    }

<强>更新

要将颜色重置为默认值,只需在ClearValue(DependencyProperty)实例上调用MyButton

    private void MyButton_Click(object sender, RoutedEventArgs e)
    {
        MyButton myButton = sender as MyButton;
        myButton.ClearValue(MyButton.MyColorProperty);
    }

答案 1 :(得分:1)

这个怎么样:

<Window x:Class="WpfApplication3.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication3"
Title="Window2" Height="300" Width="300">
<Window.Resources>

    <Style TargetType="my:MyButton">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=ModeCheckBox, Path=IsChecked}" Value="true">
                <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MyColor}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=ModeCheckBox, Path=IsChecked}" Value="false">
                <Setter Property="Background" Value="SkyBlue" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <CheckBox Content="Individual Mode" x:Name="ModeCheckBox" Grid.Row="0" />
    <my:MyButton Content="B1" x:Name="myB1" Grid.Row="1" MyColor="Red"/>
    <my:MyButton Content="B2" x:Name="myB2" Grid.Row="2"/>
    <my:MyButton Content="B3" x:Name="myB3" Grid.Row="3" MyColor="Green"/>

</Grid>

</Window>

我也更改了MyButton类:

public class MyButton : Button
{
    public Brush MyColor { get; set; }
}