【问题标题】:Binding itemsource with ObservableCollection [duplicate]将 itemsource 与 ObservableCollection 绑定
【发布时间】:2012-07-13 06:25:04
【问题描述】:

可能重复:
WPF listbox empty datatemplate

我的 Itemscontrol 的 Itemsource 绑定到 ObservableCollection。如果 ObservableCollection 中没有对象,如何编写代码以显示文本“列表为空”。

<ItemsControl Grid.Row="2" Name="itemsControl2" ItemsSource="{Binding RecentPatients}">

【问题讨论】:

    标签: c# silverlight xaml mvvm


    【解决方案1】:

    您也可以在 C# 视图模型中执行此逻辑。无需更改您的 xaml 代码。

    public sealed partial class MainPage : Page, INotifyPropertyChanged {
        private ObservableCollection<string> recentPatients = new ObservableCollection<string>();
        private IList<string> emptyList = new string[] { "This list is empty" };
    
        public MainPage()   {
            this.InitializeComponent();
            this.DataContext = this;
            this.recentPatients.CollectionChanged += OnCollectionChanged;
        }
        public IList<string> RecentPatients {
            get { return recentPatients.Any() ? recentPatients : emptyList; }
        }
        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)     {
            if (this.recentPatients.Count <= 1) {
                // It could be a change between empty to non-empty.
                this.OnPropertyChanged("RecentPatients");
            }
        }
        // implement the INotifyPropertyChanged pattern here.
    

    【讨论】:

      【解决方案2】:

      我为此使用了一种样式

      <Style x:Key="{x:Type ItemsControl}" TargetType="{x:Type ItemsControl}">
          <Style.Triggers>
              <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                  <Setter Property="Background">
                      <Setter.Value>
                          <VisualBrush Stretch="None">
                              <VisualBrush.Visual>
                                  <TextBlock Text="The list is empty" 
                                             FontFamily="{StaticResource FontFamily}"
                                             FontSize="{StaticResource FontSize}"/>
                              </VisualBrush.Visual>
                          </VisualBrush>
                      </Setter.Value>
                  </Setter>
              </DataTrigger>
              <DataTrigger Binding="{Binding Items, RelativeSource={RelativeSource Self}}" Value="{x:Null}">
                  <Setter Property="Background">
                      <Setter.Value>
                          <VisualBrush Stretch="None">
                              <VisualBrush.Visual>
                                  <TextBlock Text="The list is empty" 
                                             FontFamily="{StaticResource FontFamily}"
                                             FontSize="{StaticResource FontSize}"/>
                              </VisualBrush.Visual>
                          </VisualBrush>
                      </Setter.Value>
                  </Setter>
              </DataTrigger>
          </Style.Triggers>
      </Style>
      

      【讨论】:

        【解决方案3】:

        你可能会使用TargetNullValue

        见:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue

        你可以像这样添加

        <ItemsControl Grid.Row="2" Name="itemsControl2" ItemsSource="{Binding RecentPatients, TargetNullValue=The list is empty}">
        

        【讨论】:

        猜你喜欢
        • 2018-01-21
        • 2013-01-31
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 2014-05-10
        相关资源
        最近更新 更多