【问题标题】:Fire trigger in UserControl based on DependencyProperty基于 DependencyProperty 的 UserControl 中的触发触发器
【发布时间】:2013-07-05 14:18:13
【问题描述】:

有一个非常简单的问题,但我似乎无法在 Internet 上找到答案。可能是因为我没有找对地方。

我有一个具有自定义枚举类型的 DependencyProperty 的用户控件。 在 XAML 中,我想根据枚举类型的值显示/隐藏元素。我尝试使用 DataTriggers 执行此操作,但无法正常工作。

<UserControl x:Class="WpfApplication1.DisplayIcon"
         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="50" d:DesignWidth="50"
         x:Name="control">
<UserControl.Resources>
    <Style TargetType="Ellipse">
        <Setter Property="Visibility" Value="Collapsed"/>
        <Style.Triggers>
            <DataTrigger Value="Ellipse" Binding="{Binding MyIconType, ElementName=control}">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="Rectangle">
        <Setter Property="Visibility" Value="Collapsed"/>
        <Style.Triggers>
            <DataTrigger Value="Rectangle" Binding="{Binding MyIconType, ElementName=control}">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</UserControl.Resources>
<Grid>
    <Ellipse x:Name="el1" Fill="Red" Width="30" Height="30" />
    <Rectangle x:Name="el2" Fill="Green" Width="20" Height="20" /> 
    <TextBlock Text="{Binding MyIconType, ElementName=control}" Margin="0,40,0,0"/>
</Grid></UserControl>

我的代码如下所示:

public enum IconType
{
    Ellipse,
    Rectangle
}
public partial class DisplayIcon : UserControl
{
    public DisplayIcon()
    {
        InitializeComponent();
    }

    public IconType MyIconType
    {
        get { return (IconType)GetValue(MyIconTypeProperty); }
        set { SetValue(MyIconTypeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyIconType.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyIconTypeProperty =
        DependencyProperty.Register("MyIconType", typeof(IconType), typeof(DisplayIcon), new PropertyMetadata(IconType.Ellipse));

}

有人可以帮我吗?

谢谢,

吉姆

【问题讨论】:

    标签: wpf user-controls triggers dependency-properties


    【解决方案1】:

    您可以为每个元素创建一个Style 并在那里定义触发器:

    <UserControl x:Class="WpfApplication1.DisplayIcon"
         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="50" d:DesignWidth="50"
         x:Name="control">
    
    <UserControl.Resources>
    
        <Style TargetType="Ellipse">
            <Style.Triggers>
                <DataTrigger Value="Rectangle" Binding="{Binding MyIconType, ElementName=control}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    
        <Style TargetType="Rectangle">
            <Style.Triggers>
                <DataTrigger Value="Ellipse" Binding="{Binding MyIconType, ElementName=control}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    
    </UserControl.Resources>
    
    <Grid>
        <Ellipse x:Name="el1" Fill="Red" Width="20" Height="20"/>
        <Rectangle Grid.Row="1" x:Name="el2" Fill="Green" Width="20" Height="20"/>
    </Grid>
    

    编辑:

    事实上,反转Visibility 逻辑可能更有意义。这样您就可以添加形状而无需修改代码:

    <UserControl x:Class="WpfApplication1.DisplayIcon"
         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="50" d:DesignWidth="50"
         x:Name="control">
    
    <UserControl.Resources>
    
        <Style TargetType="Ellipse">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Value="Ellipse" Binding="{Binding MyIconType, ElementName=control}">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    
        <Style TargetType="Rectangle">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Value="Rectangle" Binding="{Binding MyIconType, ElementName=control}">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    
    </UserControl.Resources>
    
    <Grid>
        <Ellipse x:Name="el1" Fill="Red" Width="20" Height="20"/>
        <Rectangle x:Name="el2" Fill="Green" Width="20" Height="20"/>
    </Grid>
    

    【讨论】:

    • 感谢您的回复,但这似乎也不起作用。触发器不会被触发。
    • 我复制了你的代码,但它不起作用。你有工作副本吗?
    • 我通过更改按钮单击处理程序中的 MyIconType 值对其进行了测试。对我有用: private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { if (MyIconType == IconType.Ellipse) MyIconType = IconType.Rectangle;否则 { MyIconType = IconType.Ellipse; } }
    • 我在带有以下 XAML 的 MainWindow 中使用它(但它不起作用)。有什么线索吗? schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace :WpfApplication1">
    • 不确定。我复制了您的 MainWindow 代码,它只显示了椭圆。将 MyIconType 更改为“Rectangle”,它只显示了 Rectangle。
    猜你喜欢
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2015-03-26
    • 2010-10-19
    • 1970-01-01
    • 2015-10-14
    • 2020-11-23
    相关资源
    最近更新 更多