【问题标题】:List box with multiple data template - style for selected item具有多个数据模板的列表框 - 所选项目的样式
【发布时间】:2014-03-22 13:19:07
【问题描述】:

我有带有 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;
                }
            }
        }

    }

【问题讨论】:

  • 如果从 NewDataTemplate 中删除 Background="{Binding... 是否有效?还要发布转换器的代码以及项目类的代码。
  • 是的,如果我从 NewDataTemplate 中删除 Background ="{Binding...,ListItemSelectorStyle 工作正常。但我需要那个转换器,因为我需要在列表框中的 4 个项目中突出显示 2 个项目,我将分享我的转换器代码
  • 例如,您的商品有.IsCorrect == 1,并显示为绿色。如果单击该项目会发生什么?现在会发生什么?
  • @vorrtex 如果我单击该项目,样式颜色即&lt;VisualState x:Name="Selected"&gt; 将应用于它
  • 现在应用了吗?你想应用Selected 状态吗?还是您宁愿不应用它?

标签: c# wpf silverlight listbox windows-phone


【解决方案1】:

您的第二个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}}"

  1. 您没有提供任何路径。因此,您的转换器不会获得任何输入值。
  2. 另外,您应该在上述绑定中指定参数。

看看下面的例子:

首先在您的 ViewModel 中声明如下属性:

private MyObject myBackground;
public MyObject MyBackground
{
    get
    {
        return myBackground;
    }
    set
    {
        myBackground = value;
        NotifyPropertyChanged("MyBackground");
    }
}

在更改 DataTemplate 或 ViewModel 的构造函数之前填充 MyBackground 中的值。

在您的数据模板中:

<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;
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多