样式化自定义用户控件已禁用状态

时间:2015-12-15 17:12:52

标签: c# wpf xaml

我如何设定“残疾人”的风格?我的自定义用户控件的状态?我不清楚如何设置自定义用户控件的各个部分的样式。例如,我的自定义用户控件由几个组件组成。那么如何在全局样式表中定位控件的特定部分?请记住,如果它再次启用,我希望它恢复原来的颜色。

这是我的自定义用户控制代码......

VNode.xaml

<UserControl x:Class="WpfApplication1.VNode"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="200">
    <Grid>
        <Rectangle x:Name="Backplate" Fill="Green" Width="100" Height="50" RadiusX="3" RadiusY="3"/>
        <Rectangle x:Name="Highlight"
                   Height="60"
                   Width="110" 
                   Fill="Transparent"
                   Stroke="White"
                   StrokeThickness="2"
                   RadiusX="6" RadiusY="6">
        </Rectangle>
        <TextBlock x:Name="Label" Text="Label" TextWrapping="Wrap" Width="100" Height="50"/>
    </Grid>
</UserControl>

VNode.xaml.cs

using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for VNode.xaml
    /// </summary>
    public partial class VNode : UserControl
    {
        public VNode()
        {
            InitializeComponent();
        }
        public Brush BackplateColor
        {
            get { return Backplate.Fill; }
            set { Backplate.Fill = value; }
        }
        public string Text
        {
            get { return Label.Text; }
            set { Label.Text = value; }
        }
    }
}

样式表

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication1">

    <Style x:Key="NodeStyle" TargetType="{x:Type local:VNode}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Backplate" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

1 个答案:

答案 0 :(得分:2)

最好为每个Style创建或覆盖Control。它为您提供了Controls来自代码隐藏的更改样式的灵活性,这是一个关注点的分离。

例如,让我们看一下TextBlock的样式,它的样式取决于IsMouseOverIsEnabled条件:

在App.xaml文件中:

<Application x:Class="UWPWpfApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="TextBlock" x:Key="VNodeTextBlock">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontSize" Value="8"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="20" />
                </Trigger>
            </Style.Triggers>
        </Style>

    </Application.Resources>
</Application>

表格:

<TextBlock Text="Hello World!:)" Style="{StaticResource VNodeTextBlock}"/>