所选项目具有多个数据模板样式的列表框
本文关键字:样式 列表 数据 项目 选项 | 更新日期: 2023-09-27 17:51:23
我有2个数据模板的列表框,我有一个列表框的ItemContainerStyle,它将突出显示列表框中的选定项目。
下面是我的代码:<DataTemplate x:Key="DefaultDataTemplate">
<Border
Margin="0,2,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid> items ...</Grid>
</Border>
</DataTemplate>
带转换器的数据模板:
<DataTemplate x:Key="NewDataTemplate">
<Border
Background="{Binding Converter={StaticResource BackgroundConvertor}}"
Margin="0,2,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid> items ...</Grid>
</Border>
</DataTemplate>
我在应用程序栏上有一个按钮,点击该按钮,我以编程方式设置NewDataTemplate which will change 2 item colors to green in the list box
。
列表框项选择器样式:
<Style x:Key="ListItemSelectorStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property ="Foreground" Value="Black" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="ListBoxItem" Background="{TemplateBinding Background}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ListItemBorder"
BorderBrush="Transparent" Background="#e3e8f0">
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这将在我们选择项目时应用样式。
现在这个样式在我的DefaultDataTemplate上工作得很好,当我点击列表框中的项目时,这意味着该项目得到突出显示,但是当设置了NewDataTemplate时,样式根本不显示。
如何解决这个问题?
注意:我正在开发Windows Phone 8应用程序。
编辑1
public class BackgroundConvertor: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush solidColorBrush = null;
if (value != null)
{
MyObject obj = value as MyObject ;
if (parameter != null)
{
if (obj.IsCorrect == 1 && parameter.ToString() == "0")
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)235, (byte)242)); //blue color
}
else if (obj.IsCorrect == 1 && parameter.ToString() == "1")
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)242, (byte)169));//green color
}
else
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)227, (byte)232, (byte)240));//Grey color.
}
}
else if (obj.IsCorrect == 1)
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)242, (byte)169));//green color
}
else
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)227, (byte)232, (byte)240));//Grey color.
}
}
return solidColorBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
编辑2
这是我的MyObject类:
public class MyObject
{
private byte isCorrect;
public byte IsCorrect
{
get { return isCorrect; }
set
{
if (value != this.isCorrect)
{
isCorrect = value;
}
}
}
}
您的第二个DataTemplate
有几个问题。
下面代码:
<DataTemplate x:Key="NewDataTemplate">
<Border
Background="{Binding Converter={StaticResource BackgroundConvertor}}"
Margin="0,2,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid> items ...</Grid>
</Border>
</DataTemplate>
在上面的代码中:
Background="{Binding Converter={StaticResource BackgroundConvertor}}"
- 没有提供任何路径。因此,您的转换器将无法获得任何输入值。
- 同样,您应该指定上述绑定中的参数。
看下面的例子:
首先在ViewModel中声明如下属性:
private MyObject myBackground;
public MyObject MyBackground
{
get
{
return myBackground;
}
set
{
myBackground = value;
NotifyPropertyChanged("MyBackground");
}
}
在更改DataTemplate或ViewModel的构造函数之前填充MyBackground中的值。
在你的DataTemplate:
<DataTemplate x:Key="NewDataTemplate">
<Border
Background="{Binding Path=MyBackground, Converter={StaticResource BackgroundConvertor}
ConverterParameter='1'}" />
Margin="0,2,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid> items ...</Grid>
</Border>
</DataTemplate>
您在上面代码中指定的转换器也应该在这里使用。
注意:上面的示例代码没有经过测试。如果有任何错误,请尝试解决。如果您有什么问题,请尽管提。
更新:
你不需要对你的Answer
类做任何改变。
在你的ViewModel中像下面这样声明一个属性:
private Answer myBackground;
public Answer MyBackground
{
get
{
return myBackground;
}
set
{
myBackground = value;
OnPropertyChanged("MyBackground");
}
}
使用我在前面的回答中提到的XAML。
对您的转换器进行如下更改:
public class BackgroundConvertor: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush solidColorBrush = null;
if (value != null)
{
Answer answer = (Answer)value ;
if (parameter != null)
{
if (answer.IsCorrect == 1 && parameter.ToString() == "0")
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)235, (byte)242)); //blue color
}
else if (answer.IsCorrect == 1 && parameter.ToString() == "1")
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)242, (byte)169));//green color
}
else
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)227, (byte)232, (byte)240));//Grey color.
}
}
else if (answer.IsCorrect == 1)
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)201, (byte)242, (byte)169));//green color
}
else
{
solidColorBrush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)227, (byte)232, (byte)240));//Grey color.
}
}
return solidColorBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}