none
TreeViewItem怎么设置被点击时的触发器(不是IsSelected)? RRS feed

答案

  • 请使用EventTrigger

    参考代码

      <TreeView x:Name="TV"  >
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="MouseLeftButtonUp">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(TreeViewItem.IsEnabled)">
                                            <BooleanKeyFrameCollection>
                                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1"/>
                                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:2"/>
                                            </BooleanKeyFrameCollection>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock >
                            <Run Text="{Binding Name}"/>
                            <Run Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor ,AncestorLevel=1, AncestorType=TreeViewItem}, Path=IsEnabled}"/>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

     /*********************CS代码***********************/
    public partial class MainWindow : Window
        {
            List<TestClass> li;
            public MainWindow()
            {
                li = new List<TestClass>();
                InitializeComponent();
                for (var i = 0; i < 100; i++)
                    li.Add(new TestClass() {Name="TreeViewItem Count:"+i.ToString(),IsDoClick=true });
                TV.ItemsSource = li;
            }   
        }
    /***************这个数据类*****************/
        public class TestClass:INotifyPropertyChanged
        {
            public string Name { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
    
            private bool _b;
    
            public bool IsDoClick { get => _b; set { _b = value;OnChanged(new PropertyChangedEventArgs("IsDoClick")); } }
        }

    2019年1月16日 12:54
  • 请使用Xmlns的定义的名字+类名+属性即可

    我修改了一下上面代码的

      <Grid>
            <TreeView x:Name="TV"  >
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="local:TestATCH.TestBool" Value="True"/>
                        <Style.Triggers>
                            <!--此处为事件触发器-->
                            <EventTrigger RoutedEvent="MouseLeftButtonUp">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--此处动画为修改附加属性的Bool值-->
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(local:TestATCH.TestBool)">
                                            <BooleanKeyFrameCollection>
                                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1"/>
                                            </BooleanKeyFrameCollection>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <!--通过触发器检测附加属性的值-->
                            <Trigger Property="local:TestATCH.TestBool"  Value="False">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock >
                            <Run Text="{Binding Name}"/>
                            <Run Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor ,AncestorLevel=1, AncestorType=TreeViewItem}, Path=IsEnabled}"/>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
      
        </Grid>

    附加类

    using System.Windows;
    
    namespace TreeItem
    {
        public class TestATCH
        {
            public static readonly DependencyProperty TestBoolProperty = DependencyProperty.RegisterAttached("TestBool", typeof(bool), typeof(TestATCH), new PropertyMetadata(true, null));
    
            public static void SetTestBool(DependencyObject dependency, bool value) => dependency.SetValue(TestBoolProperty, value);
    
            public static bool GetTestBool(DependencyObject dependency) => (bool)dependency.GetValue(TestBoolProperty);
        }
    }
    

    2019年1月17日 3:20

全部回复

  • 请使用EventTrigger

    参考代码

      <TreeView x:Name="TV"  >
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="MouseLeftButtonUp">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(TreeViewItem.IsEnabled)">
                                            <BooleanKeyFrameCollection>
                                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1"/>
                                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:2"/>
                                            </BooleanKeyFrameCollection>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock >
                            <Run Text="{Binding Name}"/>
                            <Run Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor ,AncestorLevel=1, AncestorType=TreeViewItem}, Path=IsEnabled}"/>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

     /*********************CS代码***********************/
    public partial class MainWindow : Window
        {
            List<TestClass> li;
            public MainWindow()
            {
                li = new List<TestClass>();
                InitializeComponent();
                for (var i = 0; i < 100; i++)
                    li.Add(new TestClass() {Name="TreeViewItem Count:"+i.ToString(),IsDoClick=true });
                TV.ItemsSource = li;
            }   
        }
    /***************这个数据类*****************/
        public class TestClass:INotifyPropertyChanged
        {
            public string Name { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
    
            private bool _b;
    
            public bool IsDoClick { get => _b; set { _b = value;OnChanged(new PropertyChangedEventArgs("IsDoClick")); } }
        }

    2019年1月16日 12:54
  • 请使用EventTrigger

    参考代码

      <TreeView x:Name="TV"  >
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="MouseLeftButtonUp">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(TreeViewItem.IsEnabled)">
                                            <BooleanKeyFrameCollection>
                                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1"/>
                                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:2"/>
                                            </BooleanKeyFrameCollection>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock >
                            <Run Text="{Binding Name}"/>
                            <Run Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor ,AncestorLevel=1, AncestorType=TreeViewItem}, Path=IsEnabled}"/>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

     /*********************CS代码***********************/
    public partial class MainWindow : Window
        {
            List<TestClass> li;
            public MainWindow()
            {
                li = new List<TestClass>();
                InitializeComponent();
                for (var i = 0; i < 100; i++)
                    li.Add(new TestClass() {Name="TreeViewItem Count:"+i.ToString(),IsDoClick=true });
                TV.ItemsSource = li;
            }   
        }
    /***************这个数据类*****************/
        public class TestClass:INotifyPropertyChanged
        {
            public string Name { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
    
            private bool _b;
    
            public bool IsDoClick { get => _b; set { _b = value;OnChanged(new PropertyChangedEventArgs("IsDoClick")); } }
        }

    还想问还有1个问题,如果把 Storyboard.TargetProperty="(TreeViewItem.IsEnabled)" 引号的内容换成为TreeViewItem扩展的bool类型的附加属性,那要怎么写?打"命名空间简化名称+类名+附加属性全名"不行



    2019年1月17日 1:43
  • 请使用Xmlns的定义的名字+类名+属性即可

    我修改了一下上面代码的

      <Grid>
            <TreeView x:Name="TV"  >
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="local:TestATCH.TestBool" Value="True"/>
                        <Style.Triggers>
                            <!--此处为事件触发器-->
                            <EventTrigger RoutedEvent="MouseLeftButtonUp">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--此处动画为修改附加属性的Bool值-->
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(local:TestATCH.TestBool)">
                                            <BooleanKeyFrameCollection>
                                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1"/>
                                            </BooleanKeyFrameCollection>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <!--通过触发器检测附加属性的值-->
                            <Trigger Property="local:TestATCH.TestBool"  Value="False">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock >
                            <Run Text="{Binding Name}"/>
                            <Run Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor ,AncestorLevel=1, AncestorType=TreeViewItem}, Path=IsEnabled}"/>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
      
        </Grid>

    附加类

    using System.Windows;
    
    namespace TreeItem
    {
        public class TestATCH
        {
            public static readonly DependencyProperty TestBoolProperty = DependencyProperty.RegisterAttached("TestBool", typeof(bool), typeof(TestATCH), new PropertyMetadata(true, null));
    
            public static void SetTestBool(DependencyObject dependency, bool value) => dependency.SetValue(TestBoolProperty, value);
    
            public static bool GetTestBool(DependencyObject dependency) => (bool)dependency.GetValue(TestBoolProperty);
        }
    }
    

    2019年1月17日 3:20