繁体   English   中英

反向DataTrigger?

[英]Inverse DataTrigger?

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

是否有针对此类情况的机制,或者建议采用哪种方法来应对此类情况?

谢谢。

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

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

<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定义的所有ListBoxItems注册MouseEnterMouseLeave事件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的文本颜色设置为红色,当鼠标离开该项目时,将其设置为黑色。

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

如果要响应事件(如EventTrigger),则始终可以简单地在代码中订阅该事件,然后在处理程序中设置data属性。

您可以通过这种方式使用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处理程序中的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM