C# 如何将导入的ObservableCollection字符串绑定到ItemsControl
范例C# 如何将导入的ObservableCollection字符串绑定到ItemsControl,c#,wpf,C#,Wpf,范例 class abc { public ObservableCollection<string> Data { get; set; } //data will be initialized in some functions } 我正试图这样做,但这不起作用同时添加数据上下文 public List<string> Data { get; set; } <Window.DataContext> <ViewModel:ManageUse
class abc
{
public ObservableCollection<string> Data { get; set; }
//data will be initialized in some functions
}
我正试图这样做,但这不起作用同时添加数据上下文
public List<string> Data { get; set; }
<Window.DataContext>
<ViewModel:ManageUserViewModel/>
</Window.DataContext>
在cs文件中:
private ObservableCollection<UserType> _listUserTypes = new ObservableCollection<UserType>();
public ObservableCollection<UserType> UserTypes
{
set
{
_listUserTypes = value;
}
get
{
return _listUserTypes;
}
}
private observeCollection\u listUserTypes=new observeCollection();
公共ObservableCollection用户类型
{
设置
{
_listUserTypes=值;
}
得到
{
返回_listUserTypes;
}
}
在Xaml中:
<ItemsControl ItemsSource="{Binding Path=UserTypes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
WPF,我想知道如何在编写代码后绑定它。如果您以任何方式编写代码,您可以直接设置ItemsSource。databox.ItemsSource=\u abc.Data;在codebehind中设置绑定有什么问题?它不起作用,我应该在ItemsControl中为TextBlosk设置什么?您必须设置DataContext,关于导航事件。我修改了您不能在导航事件中设置的回答否,因为observable collection位于其他类中,并在wpf中导入。请再次查看问题,我只能在OnImportSusfitured()中设置上下文。我猜您没有遵循MVVM的方式,然后按照上面的方式操作!没有im遵循MVVM的方式,这件事在textblock和其他方面都很好,但只有在Itemscontrol的情况下,我无法做到这一点
<Window.DataContext>
<ViewModel:ManageUserViewModel/>
</Window.DataContext>
private ObservableCollection<UserType> _listUserTypes = new ObservableCollection<UserType>();
public ObservableCollection<UserType> UserTypes
{
set
{
_listUserTypes = value;
}
get
{
return _listUserTypes;
}
}
<ItemsControl ItemsSource="{Binding Path=UserTypes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>