Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将itemsource与ObservableCollection绑定_C#_Silverlight_Xaml_Mvvm - Fatal编程技术网

C# 将itemsource与ObservableCollection绑定

C# 将itemsource与ObservableCollection绑定,c#,silverlight,xaml,mvvm,C#,Silverlight,Xaml,Mvvm,可能重复: my Items控件的Itemsource绑定到ObservableCollection。如果ObservableCollection中没有对象,如何编写代码以显示文本“列表为空” <ItemsControl Grid.Row="2" Name="itemsControl2" ItemsSource="{Binding RecentPatients}"> 您可能可以使用TargetNullValue 见: 你可以像这样添加它 <ItemsControl Grid

可能重复:

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

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

您可能可以使用
TargetNullValue

见:

你可以像这样添加它

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

我使用了一种样式

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

您也可以在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.
公共密封部分类主页面:第页,INotifyPropertyChanged{
私人可观察收集最近患者=新可观察收集();
private IList emptyList=新字符串[]{“此列表为空”};
公共主页(){
this.InitializeComponent();
this.DataContext=this;
this.recentPatients.CollectionChanged+=OnCollectionChanged;
}
公立医院近期病人{
获取{return recentPatients.Any()?recentPatients:emptyList;}
}
CollectionChanged的私有void(对象发送方,NotifyCollectionChangedEventArgs e){

如果(this.recentPatients.Count)很好,它是否可以使用空列表而不是空列表?很好,请选中此项: