2009-02-11 30 views
1

我可以基於使用DataTrigger像這樣的東西逆DataTrigger?

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

但是,如果我想要做什麼的對面基礎數據對象的屬性在我的一個ListBoxItem模板觸發屬性設置?我的意思是根據我的ListBoxItem的屬性值設置底層數據對象的屬性值。像這樣的:

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

是否有像這樣的機制或什麼是處理這種情況的推薦方法?

謝謝。

+0

我在示例代碼中添加了示例代碼 - 是否適合您? – Andy 2009-02-11 15:37:02

+0

是的,它的工作原理。謝謝! – 2009-02-11 16:33:28

回答

2

我認爲,你可以使用一個EventSetter在XAML做什麼喬希的G代碼建議。也許爲MouseEnterMouseLeave事件創建一個事件,併爲每個事件適當地設置控件風格?

更新:您可以設置這樣的事件:爲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> 

樣式寄存器在ListBox.

定義的所有ListBoxItems然後在後面的代碼文件:

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文本的顏色設置爲紅色,並在鼠標離開時將其移回黑色。

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

謝謝。我的問題是我無法理解如何將事件處理程序連接到ListBox的子項的MouseEnter/MouseLeave事件或確定頂級MouseEnter/MouseLeave事件中的光標(如果有)的哪個項目ListBox的? – 2009-02-11 14:31:56