Trigger for ComboBox in Silverlight
Is there any possibility to display selected item of the ComboBox (after popup closing) in a way that is different from its displaying in DropDown List (There are players number and name in the dropdown list, but after list closing I want to see only its number).
How can I change a background for the player with some Flag?
As far as I know, all of that can be done w开发者_运维技巧ith triggers, but are they supported in Silverlight 4, VS2010, Silverlight Toolkit 4? In my case the following code
<ComboBox ItemsSource="{Binding PlayersAll}"
SelectedItem="{Binding Path=SelectedPlayer, Mode=TwoWay}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<ToolkitControls:WrapPanel Orientation="Horizontal">
<TextBlock Text="{Binding TeamNumber}"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding ShortName}"/>
</ToolkitControls:WrapPanel>
<DataTemplate.Triggers>
<Trigger Property="HasError" Value="True">
<Setter Property="Background" TargetName="FlagSet" Value="Red"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
gives an error:
The property 'Triggers' does not exist on the type 'DataTemplate' in the XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'
what is wrong here? Here are my namespaces:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:ToolkitControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
There doesn't appear to be a way to display the selected item when the popup is closed differently. It would be a good idea though, it would require that an alternative data template be provided for this area, sadly combox doesn't do that. You would need to build your own implementation based on Selector
to do that, not a trivial task.
To bind boolean property such as your HasError
property to some other property of a different type on a control (such as the Background
property) use an implementation of IValueConverter
. You can find the code for a BoolToBrushConverter in this blog article.
You can use it something like this:-
<UserControl.Resources>
<local:BoolToBrushConverter x:Key="FlagToBrush" TrueValue="Red" FalseValue="Transparent"/>
</UserControl.Resources>
Now lets assume you mean to change the background color of the items displayed in the combobox:-
<DataTemplate>
<ToolkitControls:WrapPanel Orientation="Horizontal"
Background="{Binding HasError, Converter={StaticResource FlagToBrush}}>
<TextBlock Text="{Binding TeamNumber}"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding ShortName}"/>
</ToolkitControls:WrapPanel>
</DataTemplate>
(BTW, why WrapPanel
instead of a simple StackPanel
?)
精彩评论