首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逆向DataTrigger?

逆向DataTrigger?
EN

Stack Overflow用户
提问于 2009-02-11 07:49:32
回答 2查看 1.9K关注 0票数 1

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

代码语言:javascript
复制
<DataTrigger Binding="{Binding Path=IsMouseOver}" Value="true">
  <Setter TargetName="ItemText" Property="TextBlock.TextDecorations" Value="Underline">
  </Setter>
</DataTrigger>

但如果我想反其道而行之呢?我的意思是根据ListBoxItem的属性值在底层数据对象上设置属性值。类似于:

代码语言:javascript
复制
<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="MyClass.IsHilited" Value="True"></Setter>
</Trigger>

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

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-02-11 14:32:18

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

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

代码语言:javascript
复制
<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>

该样式为该ListBox.中定义的所有ListBoxItemsMouseEnterMouseLeave事件注册

然后在您的代码隐藏文件中:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2009-02-11 13:58:01

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

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

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

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
Binding myBind = new Binding("IsHilited");
myBind.Source = listBox.DataContext;
listBox.SetBinding(listBox.IsEnabled, myBind);

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

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

下面是一个例子:

代码语言:javascript
复制
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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/535817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档