【发布时间】:2011-02-23 17:38:57
【问题描述】:
我有一个带有以下 xaml 的 WPF 用户控件
<UserControl x:Class="Scheduler.ItemBox"
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"
mc:Ignorable="d"
d:DesignHeight="40" d:DesignWidth="150" MinHeight="40" MinWidth="75" VerticalAlignment="Top">
<Border BorderBrush="CornflowerBlue" BorderThickness="1" CornerRadius="5" Name="border">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FFC0D3EA" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid Margin="2,0" Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" MaxHeight="20" MinHeight="20" />
<RowDefinition MinHeight="20" />
</Grid.RowDefinitions>
<Label Content="00:00" FontWeight="Bold" Name="FromTime" Padding="5,0,0,0" VerticalContentAlignment="Center" />
<Label Content="01:30" Grid.Column="1" HorizontalContentAlignment="Right" Name="ToTime" Padding="0,0,5,0" VerticalContentAlignment="Center" />
<TextBlock Grid.ColumnSpan="2" Grid.Row="1" Name="MovieTitle" Padding="5,0" Text="item1" TextWrapping="Wrap" />
</Grid>
</Border>
用户控件类看起来像这样
Namespace Scheduler
Public Class ItemBox
Public Property Selected As Boolean
End Class
结束命名空间
现在我想做的是,当我将属性 Selected 更改为 True 时,如下所示: - 将边框边框笔刷设置为黑色 - 将边框边框厚度设置为 2 - 将网格边距设置为 1
我想通过在usercontrol中定义“选定的”样式来完成此操作,该样式覆盖所选属性设置为true时的默认样式。
我知道它与样式触发器和定义自定义附加属性有关。 但我似乎无法让它按我想要的方式工作。
【问题讨论】:
-
嗨,您通常会使用 VisualStateManager 来执行此类操作。看看:windowsclient.net/wpf/wpf35/… 提前一件事 - 注意 GoToState 和 GoToElementState 之间的区别:)
标签: wpf xaml properties styles