none
触发器,如何设置控件中的对象类型的依赖属性的对象子属性值变化作为条件? RRS feed

  • 问题

  • 自定义控件,控件内部有对象类型的依赖属性,该属性是集合类型,或者是其他的控件类型(自定义控件实例与其他控件实例产生依赖,比如DataGridCell,通过DataGridCell实例,找到包含它的DataGridRow),

    要将对象类型的依赖属性,其中对象的属性作为触发条件,比如集合类型,令集合属性的Count==0时作为触发条件,又或者是与自定义控件存在硬关联的控件类型实例(自定义控件内,有一个控件对象类型的依赖属性)。

    请问这种情况,触发器的条件要怎么填写,也就是触发器的Property="" 这一项要怎么填写

    2022年10月30日 1:35

答案

  • 根据 Requirements for a Custom Class as a XAML Element 在 XAML 中无法执行要执行的操作:

    自定义类不得是嵌套类。嵌套类及其常规 CLR 用法语法中的“点”会干扰其他 WPF 和/或 XAML 功能,例如附加属性。


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2022年11月10日 10:44
    2022年11月7日 2:47

全部回复

  • Hi,@Trian555 。你可以给我分享你的可以重现问题的完整的示例代码来重现问题和分析吗?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2022年11月3日 8:42
  • Hi,@Trian555 。你可以给我分享你的可以重现问题的完整的示例代码来重现问题和分析吗?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    你好,假如我自定义一个控件,其中有一个IList集合类型的依赖属性

    public class TestCustomControl : Control
        {
            static TestCustomControl()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
            }
    
            public IList TestList
            {
                get { return (IList )GetValue(TestListProperty); }
                set { SetValue(TestListProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for TestList.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty TestListProperty =
                DependencyProperty.Register("TestList", typeof(IList ), typeof(TestCustomControl), new PropertyMetadata(null));
        }

    xaml:

    <Style TargetType="{x:Type local:TestCustomControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:TestCustomControl}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                        </Border>
    
                        <ControlTemplate.Triggers>
                            <Trigger Property="local:TestCustomControl.TestList.Count" Value="0">
                                <Setter Property="Background"  Value="Green" ></Setter>
                                <Setter  Property="BorderBrush"  Value="Blue" ></Setter>
                                <Setter  Property="BorderThickness"  Value="2" ></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    //<Trigger Property="local:TestCustomControl.TestList.Count" Value="1">提示不支持的嵌套类型

    2022年11月4日 6:10
  • 根据 Requirements for a Custom Class as a XAML Element 在 XAML 中无法执行要执行的操作:

    自定义类不得是嵌套类。嵌套类及其常规 CLR 用法语法中的“点”会干扰其他 WPF 和/或 XAML 功能,例如附加属性。


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2022年11月10日 10:44
    2022年11月7日 2:47