我可以使用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>有没有这样的机制,或者有什么推荐的方法来处理这样的情况?
谢谢。
发布于 2009-02-11 14:32:18
我认为您可以使用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>该样式为该ListBox.中定义的所有ListBoxItems的MouseEnter和MouseLeave事件注册
然后在您的代码隐藏文件中:
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-11 13:58:01
WPF触发器旨在引起可视更改。触发器中的Setter对象会导致控件上的属性更改。
如果您想响应一个事件(如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。
如果您创建自定义控件,您可以拥有更大的灵活性来将类似这样的绑定构建到控件中。您可以创建自定义depency属性,并将其与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>https://stackoverflow.com/questions/535817
复制相似问题