以编程方式更改单个 ListViewItem 布局

发布于 2024-07-21 05:47:05 字数 1727 浏览 6 评论 0原文

我有一个 ListView ,其中有一个 GridView

<ListView x:Name="listViewPersons" ItemsSource="{Binding Path=Persons, Mode=OneWay}" IsSynchronizedWithCurrentItem="True" 
                  Height="Auto" HorizontalAlignment="Stretch" Margin="4,4,4,4" Grid.Row="4" VerticalAlignment="Stretch" Width="Auto">
            <ListView.View>
                <GridView x:Name="gridViewPersons">
                    <GridViewColumn Header="Enabled">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <CheckBox HorizontalAlignment="Center" IsChecked="true" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Path=Name}" Width="142" />
                    <GridViewColumn Header="Age"  DisplayMemberBinding="{Binding Path=Age}" Width="142" />
                    <GridViewColumn Header="Gender"  DisplayMemberBinding="{Binding Path=Gender}" Width="142" />
                </GridView>
            </ListView.View>
        </ListView>

我希望能够以编程方式更改每个 ListViewItem (或grid)背景或前景为我想要的任何颜色,例如

listViewPersons.Items[0].Background = Brush.Red;
listViewPersons.Items[1].Background = Brush.Blue;
listViewPersons.Items[2].Background = Brush.Green

我知道前面的代码行不起作用,但它几乎解释了我想要实现的目标。 有什么帮助吗?

谢谢!

I have a ListView with a GridView in it:

<ListView x:Name="listViewPersons" ItemsSource="{Binding Path=Persons, Mode=OneWay}" IsSynchronizedWithCurrentItem="True" 
                  Height="Auto" HorizontalAlignment="Stretch" Margin="4,4,4,4" Grid.Row="4" VerticalAlignment="Stretch" Width="Auto">
            <ListView.View>
                <GridView x:Name="gridViewPersons">
                    <GridViewColumn Header="Enabled">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <CheckBox HorizontalAlignment="Center" IsChecked="true" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Path=Name}" Width="142" />
                    <GridViewColumn Header="Age"  DisplayMemberBinding="{Binding Path=Age}" Width="142" />
                    <GridViewColumn Header="Gender"  DisplayMemberBinding="{Binding Path=Gender}" Width="142" />
                </GridView>
            </ListView.View>
        </ListView>

I want to be able to programmatically change each of the ListViewItems (or the rows in the grid) backgrounds or foregrounds to any color I want, for instance

listViewPersons.Items[0].Background = Brush.Red;
listViewPersons.Items[1].Background = Brush.Blue;
listViewPersons.Items[2].Background = Brush.Green

I know the previous lines of code don't work, but it pretty much explains what I want to achieve. Any help?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-07-28 05:47:05

这种方法怎么样:

  1. 将 ForegroundColor 和背景颜色绑定到 Persons 中的属性。 我们将此属性称为 PersonStatus,并将其类型设置为状态枚举,仅用于此示例。
  2. 确保此人员实现 INotifyPropertyChanged,并且此属性的设置器引发 ProperyChanged 事件。 该属性将如下所示:

    公共状态 PersonStatus 
      { 
          得到 
          { 
              返回状态; 
          } 
          放 
          { 
              if(值!=状态) 
              { 
                  状态=值; 
                  if (属性更改!= null) 
                      PropertyChanged(this, new PropertyChangedEventArgs("PersonStatus")); 
              } 
          } 
      } 
      
  3. 创建一个 IValueConverter 接受 Status 类型并返回 Brushes.Black、Brushes.Red 等。如下所示:

    公共类 StatusToColorConverter : IValueConverter  
      { 
          公共对象转换(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化) 
          { 
              if (目标类型!= typeof(状态)) 
                  throw new InvalidOperationException("targetType 必须是 Status"); 
    
              状态status = (Status)值; 
    
              开关(状态) 
              { 
                  案例状态。新: 
                      返回画笔.黑色; 
                  案例状态.专业: 
                      返回 Brushes.Blue; 
                  案例状态。删除: 
                      返回 Brushes.Red; 
                  默认: 
                      返回画笔.黑色; 
              } 
          } 
    
          公共对象ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化) 
          { 
              抛出新的NotImplementedException(); 
          }  
      } 
      

How about this approach:

  1. Bind ForegroundColor and Background color to a property in Persons. Let's call this property PersonStatus, and set its type to a status enum, just for this example.
  2. Ensure that this Persons implements INotifyPropertyChanged, and the setter for this property raises a ProperyChanged event. The property will look like this:

    public Status PersonStatus
    {
        get
        {
            return status;
        }
        set
        {
            if (value != status)
            {
                status= value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("PersonStatus"));
            }
        }
    }
    
  3. Create an IValueConverter that takes the type of Status and returns a Brushes.Black, Brushes.Red, etc. Something like this:

    public class StatusToColorConverter : IValueConverter 
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Status))
                throw new InvalidOperationException("targetType must be Status");
    
            Status status = (Status)value;
    
            switch (status)
            {
                case Status.New:
                    return Brushes.Black;
                case Status.Professional:
                    return Brushes.Blue;
                case Status.Delete:
                    return Brushes.Red;
                default:
                    return Brushes.Black;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        } 
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文