<tfoot id='fpFOt'></tfoot>

<small id='fpFOt'></small><noframes id='fpFOt'>

    • <bdo id='fpFOt'></bdo><ul id='fpFOt'></ul>

        <i id='fpFOt'><tr id='fpFOt'><dt id='fpFOt'><q id='fpFOt'><span id='fpFOt'><b id='fpFOt'><form id='fpFOt'><ins id='fpFOt'></ins><ul id='fpFOt'></ul><sub id='fpFOt'></sub></form><legend id='fpFOt'></legend><bdo id='fpFOt'><pre id='fpFOt'><center id='fpFOt'></center></pre></bdo></b><th id='fpFOt'></th></span></q></dt></tr></i><div id='fpFOt'><tfoot id='fpFOt'></tfoot><dl id='fpFOt'><fieldset id='fpFOt'></fieldset></dl></div>

        <legend id='fpFOt'><style id='fpFOt'><dir id='fpFOt'><q id='fpFOt'></q></dir></style></legend>
      1. 列表框项目WPF,不同项目的不同背景颜色

        Listbox item WPF, different background color for different items(列表框项目WPF,不同项目的不同背景颜色)
          • <bdo id='dejXU'></bdo><ul id='dejXU'></ul>
            <tfoot id='dejXU'></tfoot>

                <small id='dejXU'></small><noframes id='dejXU'>

                <i id='dejXU'><tr id='dejXU'><dt id='dejXU'><q id='dejXU'><span id='dejXU'><b id='dejXU'><form id='dejXU'><ins id='dejXU'></ins><ul id='dejXU'></ul><sub id='dejXU'></sub></form><legend id='dejXU'></legend><bdo id='dejXU'><pre id='dejXU'><center id='dejXU'></center></pre></bdo></b><th id='dejXU'></th></span></q></dt></tr></i><div id='dejXU'><tfoot id='dejXU'></tfoot><dl id='dejXU'><fieldset id='dejXU'></fieldset></dl></div>

                    <tbody id='dejXU'></tbody>
                1. <legend id='dejXU'><style id='dejXU'><dir id='dejXU'><q id='dejXU'></q></dir></style></legend>

                  本文介绍了列表框项目WPF,不同项目的不同背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 WPF ListBox,其中包含来自我拥有的特定类的项目的绑定列表.像这样的:

                  I have a WPF ListBox containing a binded list of items from a specific class that I have. Something like this:

                      ObservableCollection<MyTable> tables = new ObservableCollection<MyTable>();
                  ...
                      listTables.ItemsSource = tables;
                  

                  还有 XAML:

                  <ListBox HorizontalAlignment="Left" Margin="8,10,0,0" Name="listTables" Width="153" ItemsSource="{Binding tables}" SelectionChanged="listTables_SelectionChanged" Height="501" VerticalAlignment="Top">
                          <ListBox.ItemTemplate>
                              <DataTemplate>
                                  <Grid Margin="1">
                                      <TextBlock Grid.Column="1" Text="{Binding tableName}" />
                                  </Grid>
                              </DataTemplate>
                          </ListBox.ItemTemplate>
                      </ListBox>
                  

                  一切正常.我现在要做的是根据类的某个属性为 ListBox 中的每个项目设置不同的背景.例如,假设 MyTable 类有一个名为 isOccupied 的属性.如果为某个项目设置了此标志,我希望它在 ListBox 中具有红色背景,如果不是,那么我希望它具有绿色背景.如果属性发生变化,那么背景也应该随之变化.

                  All works fine. What I want to do now is have a different background for each item in the ListBox depending on a certain property of the class. For example, let's say that the MyTable class has a property called isOccupied. If this flag is set for a certain item, I want it to have a red background in the ListBox, if it's not, then I want to have it with a green background. If the property changes, then the background should change accordingly.

                  关于如何实现这一点的任何提示?我目前正在查找有关 ItemContainerStyle 的一些信息,但我对此比较陌生,所以我不确定我是否走在正确的道路上.

                  Any tips on how to achieve this? I'm looking up some information regarding ItemContainerStyle at the moment but I'm relatively new to this so I'm not sure if I'm following the right path.

                  推荐答案

                  您可以使用 数据触发器s

                  You can achieve that using DataTriggers

                  <ListBox.ItemTemplate>
                      <DataTemplate>
                  
                          <!-- Step #1: give an x:Name to this Grid -->
                          <Grid Margin="1" x:Name="BackgroundGrid">
                              <TextBlock Grid.Column="1" Text="{Binding tableName}" />
                          </Grid>
                  
                          <!-- Step #2: create a DataTrigger that sets the Background of the Grid, depending on the value of IsOccupied property in the Model -->             
                          <DataTemplate.Triggers>
                              <DataTrigger Binding="{Binding IsOccupied}" Value="True">
                                  <Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
                              </DataTrigger>
                  
                              <DataTrigger Binding="{Binding IsOccupied}" Value="False">
                                  <Setter TargetName="BackgroundGrid" Property="Background" Value="Green"/>
                              </DataTrigger>
                          </DataTemplate.Triggers>
                      </DataTemplate>
                  </ListBox.ItemTemplate>
                  

                  请记住,如果您希望这些值在运行时发生变化,您的数据项必须正确实现并引发 Property Change Notifications:

                  Keep in mind that if you expect these values to change at runtime, your data item must properly implement and raise Property Change Notifications:

                  public class MyTable: INotifyPropertyChanged //Side comment: revise your naming conventions, this is not a table.
                  {
                     private bool _isOccupied;
                     public bool IsOccupied
                     {
                         get { return _isOccupied; }
                         set
                         {
                             _isOccupied = value;
                             NotifyPropertyChange("IsOccupied");
                         }
                      }
                  
                      //.. Other members here..
                  }
                  

                  这篇关于列表框项目WPF,不同项目的不同背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <i id='q4HZu'><tr id='q4HZu'><dt id='q4HZu'><q id='q4HZu'><span id='q4HZu'><b id='q4HZu'><form id='q4HZu'><ins id='q4HZu'></ins><ul id='q4HZu'></ul><sub id='q4HZu'></sub></form><legend id='q4HZu'></legend><bdo id='q4HZu'><pre id='q4HZu'><center id='q4HZu'></center></pre></bdo></b><th id='q4HZu'></th></span></q></dt></tr></i><div id='q4HZu'><tfoot id='q4HZu'></tfoot><dl id='q4HZu'><fieldset id='q4HZu'></fieldset></dl></div>
                  • <bdo id='q4HZu'></bdo><ul id='q4HZu'></ul>

                          <small id='q4HZu'></small><noframes id='q4HZu'>

                        • <legend id='q4HZu'><style id='q4HZu'><dir id='q4HZu'><q id='q4HZu'></q></dir></style></legend>
                              <tbody id='q4HZu'></tbody>

                            <tfoot id='q4HZu'></tfoot>