代码之家  ›  专栏  ›  技术社区  ›  Alan Mendelevich

反向数据触发器?

  •  1
  • Alan Mendelevich  · 技术社区  · 16 年前

    我可以基于底层数据对象的属性,使用DataTrigger在ListBoxItem模板上触发属性设置,如下所示

    <DataTrigger Binding="{Binding Path=IsMouseOver}" Value="true">
      <Setter TargetName="ItemText" Property="TextBlock.TextDecorations" Value="Underline">
      </Setter>
    </DataTrigger>
    

    但是如果我想做相反的事情呢?我的意思是根据我的ListBoxItem的属性值在基础数据对象上设置属性值。比如:

    <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="MyClass.IsHilited" Value="True"></Setter>
    </Trigger>
    

    有这样的机制吗?或者建议采用什么方法来处理这种情况?

    谢谢

    2 回复  |  直到 13 年前
        1
  •  2
  •   Andy    16 年前

    我想你需要一个 EventSetter 在XAML中执行Josh G在代码中建议的操作。也许可以为客户创建一个 MouseEnter MouseLeave 事件,并为每个事件适当设置控件样式?

    使现代化

    <ListBox>
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <EventSetter Event="MouseEnter" Handler="OnListBoxItemMouseEnter" />
                <EventSetter Event="MouseLeave" Handler="OnListBoxItemMouseLeave" />
            </Style>
        </ListBox.Resources>
        <ListBoxItem>Item 1</ListBoxItem>
        <ListBoxItem>Item 2</ListBoxItem>
        <ListBoxItem>Item 3</ListBoxItem>
        <ListBoxItem>Item 4</ListBoxItem>
        <ListBoxItem>Item 5</ListBoxItem>
    </ListBox>
    

    该样式适用于 滑鼠 所有人的活动 ListBoxItems 定义在 ListBox.

    然后在代码隐藏文件中:

    private void OnListBoxItemMouseEnter(object sender, RoutedEventArgs e)
    {
        ListBoxItem lvi = sender as ListBoxItem;
        if(null != lvi)
        {
            lvi.Foreground = new SolidColorBrush(Colors.Red);
        }
    }
    
    private void OnListBoxItemMouseLeave(object sender, RoutedEventArgs e)
    {
        ListBoxItem lvi = sender as ListBoxItem;
        if(null != lvi)
        {
            lvi.Foreground = new SolidColorBrush(Colors.Black);
        }
    }
    

    ListBoxItem 当鼠标位于项目上方时变为红色,当鼠标离开项目时变为黑色。

        2
  •  1
  •   Josh G    16 年前

    WPF触发器用于引起视觉变化。触发器中的Setter对象导致控件上的属性更改。

    你可以这样使用MouseEnter和MouseLeave。例如:

    listBox.MouseEnter += listBox_MouseEnter;
    listBox.MouseLeave += listBox_MouseLeave;
    
    void listBox_MouseEnter(object sender, MouseEventArgs e)
    {
        listBox.MyClass.IsHilited = true;
    }
    
    void listBox_MouseLeave(object sender, MouseEventArgs e)
    {
        listBox.MyClass.IsHilited = false;
    }
    

    可以将数据对象的属性绑定到控件上的某些属性,如:

    Binding myBind = new Binding("IsHilited");
    myBind.Source = listBox.DataContext;
    listBox.SetBinding(listBox.IsEnabled, myBind);
    

    如果创建自定义控件,则可以更灵活地将这样的绑定构建到控件中。您可以创建自定义Dependency属性,并将其与DependencyPropertyChanged处理程序中的data属性同步。然后可以使用WPF触发器设置此依赖项属性。

    public static readonly DependencyProperty IsHilitedProperty =
        DependencyProperty.Register("IsHilited", typeof(bool), typeof(CustomListBox),
        new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsHilitedChanged)));
    
    public double IsHilited
    {
        get
        {
            return (bool)GetValue(IsHilitedProperty);
        }
        set
        {
            SetValue(IsHilitedProperty, value);
        }
    }
    
    
    private static void OnIsHilitedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        CustomListBox box = obj as CustomListBox;
    
        if (box != null)
            box.MyClass.IsHilited = box.IsHilited;
    
       // Or:
       // Class myClass = box.DataContext as Class;
       // myClass.IsHilited = box.isHilited;
    }
    
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="IsHilited" Value="True"/>
    </Trigger>