List box with multiple data template - style for selected item

后端 未结 1 1691
借酒劲吻你
借酒劲吻你 2020-12-04 04:14

I have List Box with 2 data template, i have a ItemContainerStyle for list box as well which will highlight the selected item in the list box.

Below is my code:

相关标签:
1条回答
  • 2020-12-04 05:06

    There are couple of issue with your 2nd DataTemplate.

    In Below Code:

    <DataTemplate x:Key="NewDataTemplate">
        <Border  
                Background="{Binding Converter={StaticResource BackgroundConvertor}}"
                Margin="0,2,0,0" 
                VerticalAlignment="Stretch"
                HorizontalAlignment="Stretch">
            <Grid> items ...</Grid>
       </Border>
    </DataTemplate>
    

    In above code look at :

    Background="{Binding Converter={StaticResource BackgroundConvertor}}"

    1. You are not providing any path. Thus your converter will not get any input value.
    2. Also, you should specify the parameters in above binding.

    Take a look at below example:

    First in your ViewModel declare a property like below :

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

    Fill the values in MyBackground before changing the DataTemplate or in the Constructor of your ViewModel.

    In your 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>
    

    The converter you specified in above code should be used here as well.

    Note: The above example code is not tested. If there are any errors, then please try to solve it. And if you have any problems please feel free to ask.

    Update:

    You don't need to make any changes to your Answer Class.

    In your ViewModel just declare a property like below:

    private Answer myBackground;
    public Answer MyBackground
    {
        get
        {
            return myBackground;
        }
        set
        {
            myBackground = value;
            OnPropertyChanged("MyBackground");
        }
    }
    

    Use the XAML that I mentioned previously in my Answer.

    Make changes to your converter like below code:

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题