2
假設我有一個嵌套在ListBoxItem
的Template
中的按鈕,我可以將該按鈕的IsMouseOver
屬性設置爲true,以便它看起來像它被掩蓋了嗎?WPF:我可以設置ChildElement的IsMouseOver屬性,當父母被搗毀?
只是爲了說明,窗口頂部的通知是我所指的。它基本上ListBoxItem
的一個TextBlock
和Button
假設我有一個嵌套在ListBoxItem
的Template
中的按鈕,我可以將該按鈕的IsMouseOver
屬性設置爲true,以便它看起來像它被掩蓋了嗎?WPF:我可以設置ChildElement的IsMouseOver屬性,當父母被搗毀?
只是爲了說明,窗口頂部的通知是我所指的。它基本上ListBoxItem
的一個TextBlock
和Button
不幸的是,沒有。 「IsMouseOver」是隻讀的。
我假設,雖然你有一個自定義的按鈕控制模板,對不對?如果是這種情況,一種解決方法是混亂按鈕的Tag屬性。將觸發器添加到ControlTemplate,並在設置特定的Tag值時觸發。然後,在ListBoxItems的DataTemplate中,當項目上的IsMouseOver爲true時,只需將該按鈕的Tag設置爲該特定值即可。下面是一個例子:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel x:Name="dp" Background="Transparent">
<Button x:Name="btn" DockPanel.Dock="Right" Content="x" Background="Gainsboro">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border x:Name="bd" Padding="2" BorderBrush="Black" BorderThickness="1"
Background="WhiteSmoke">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bd" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="Tag" Value="SimulatedMouseOver">
<Setter TargetName="bd" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bd" Property="Background" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text="{Binding}"/>
</DockPanel>
<DataTemplate.Triggers>
<Trigger SourceName="dp" Property="IsMouseOver" Value="True">
<Setter TargetName="btn" Property="Tag" Value="SimulatedMouseOver"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
<s:String>Item1</s:String>
<s:String>Item2</s:String>
<s:String>Item3</s:String>
</ListBox>
我收到錯誤「targetName屬性不能在風格二傳手設定」我的代碼看起來像http://pastebin.com/BUh5YLCD相關的代碼高亮顯示。 另外,但如果我有一羣我希望有的Setters,我可以將它們組合在一個樣式中,然後引用它們? – 2010-11-12 01:32:04
只能在模板中定義的觸發器(例如ControlTemplate,DataTemplate)上設置「TargetName」。我假設你的DockPanel是ListBox的DataTemplate的一部分。如果是這種情況,則需要在該模板中定義一個IsMouseOver觸發器,然後在該按鈕處移動該按鈕的Setter。 – ASanch 2010-11-12 01:58:27