【问题标题】:WPF/XAML: Switch style based on custom propertyWPF/XAML:基于自定义属性切换样式
【发布时间】: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


【解决方案1】:

第一个问题是您的 Selected 属性不是“可观察的”。这意味着任何正在监视属性更改的东西(例如样式触发器或绑定)将永远不会被通知它已更改。

您需要实现INotifyPropertyChanged 或将您的属性设为Dependency Property。它不需要是附加属性,因为如果需要,您可以使用 RelativeSource 绑定到该属性。

第二个问题是您的 UserControl 没有样式,至少默认情况下没有。即使设置了 UserControl.Style 属性,也无法轻易更改内容。这是使用custom Control 更容易完成的事情,并且是完成您想要的事情的最佳选择。

【讨论】:

  • 自定义控件的目的不是增强单个现有控件(例如:扩展具有自定义功能的按钮)吗?我正在创建的用户控件更像是一个“容器”,用于托管一些将通过单个自定义函数进行更新的控件。该控件还将接收将通知宿主窗口有关选择、拖动等的事件。在这种情况下使用自定义控件是否正确?
  • 这是自定义控件的目的之一,但您也可以创建直接从 Control 派生的自定义控件。在您的情况下,您可能希望从 ContentControl 派生,因此它更像是一个“容器”。
  • 我已经将控件重新创建为自定义控件,并为它创建了一个主题。但现在的问题是,如何在选择项目时创建第二种样式?
  • 通常,您会使用一个或多个StyleControlTemplate 触发器。然后,您可以使用 x:Name 命名您的元素,并使用触发器中的 Setter 更改它们的属性。
猜你喜欢
  • 2011-01-11
  • 2015-02-04
  • 1970-01-01
  • 2013-06-26
  • 2016-11-05
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2010-09-26
相关资源
最近更新 更多