How to set different HorizontalAlignment to ListBoxItems

前端 未结 5 1094
情歌与酒
情歌与酒 2021-01-21 12:12

I posted a question yesterday but I think I failed to explain it correctly.

Let me try again.

So this is my goal:

5条回答
  •  迷失自我
    2021-01-21 12:25

    that would be my dirty working example

    code-behind

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            var l = new List();
    
            for(int i=0;i<5;i++)
            {
            l.Add(new lItem(true,"aaa"+i));
            l.Add(new lItem(false,"bbb"+i));
            }
    
            sads.ItemsSource = l;
    
        }
    }
    
    
    public class lItem
    {
        public string Text { get; set; }
        public Brush Color { get; set; }
        public HorizontalAlignment alig { get; set; }
    
        public lItem(bool ss, string str)
        {
            Text = str;
            Color = Brushes.Blue;
            alig = HorizontalAlignment.Right;
    
            if (ss)
            {
                Color = Brushes.Red;
                alig = HorizontalAlignment.Left;
            }
        }
    }
    

    Xaml

    
                
                    
                        
                            
                                
                            
                        
                    
                
            
    

    i would recommend to use trigger instead of define visual parts in your ViewModel

提交回复
热议问题