c# - 样式自定义用户控件禁用状态

标签 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>

最佳答案

最好为每个Control 创建或覆盖Style。它使您可以灵活地从代码隐藏更改您的 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}"/>

关于c# - 样式自定义用户控件禁用状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295371/

相关文章:

c# - 在 Windows Phone silvelight 应用程序中使用 BigInteger 需要什么 namespace ?

c# - NLog ASP.Net 查看器

绑定(bind)在父属性上的 WPF DataGrid 单元格

c# - 如何访问 Windows 8.1 商店应用程序中子部分内的控件?在可视化树中搜索不起作用

wpf - 如何更改 WPF TextBlock 中文本和下划线之间的距离?

c# - 仅在一台特定机器上的 .NET 应用程序的 SSL 错误 "The message received was unexpected or badly formatted"

c# - 算法进度回调

c# - F# 类型到 Json 正在输出 Name@ 和 Name

xaml - Windows UWP Extended 初始屏幕图像在移动设备上的渲染不正确

C# webservice 将类转换为自身