gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:28 25 4
gpt4 key购买 nike

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

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com