1

我可以使用 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>

有没有这样的机制,或者处理这种情况的推荐方法是什么?

谢谢。

4

2 回答 2

2

我认为您可以使用EventSetter在 XAML 中执行 Josh G 在代码中建议的操作。也许为MouseEnterandMouseLeave事件创建一个,并为每个事件设置适当的控件样式?

更新:您可以像这样设置事件:

<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>

Style为所有定义的MouseEnterMouseLeave事件注册ListBoxItemsListBox.

然后在文件后面的代码中:

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为红色,并在鼠标离开项目时将其设置为黑色。

于 2009-02-11T14:32:17.740 回答
1

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

如果您想响应一个事件(如 EventTrigger),您总是可以简单地在代码中订阅该事件,然后在处理程序中设置数据属性。

您可以通过这种方式使用 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);

但是,您不能在绑定中使用 IsMouseOver。

如果您创建自定义控件,您可以更灵活地将这样的绑定构建到控件中。您可以创建自定义依赖属性并将其与 DependencyPropertyChanged 处理程序中的数据属性同步。然后,您可以使用 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>
于 2009-02-11T13:58:00.833 回答