如何解决将ObservableCollection <object>传递给UserControl
我一直在试图概括将ObservableCollection传递给此处提供的UserControl的解决方案:
How to bind collection dependency property in UserControl
我将UserControl背后的代码更改为:
/// <summary>
/// Interaction logic for myUserControl1.xaml
/// </summary>
public partial class myUserControl1 : UserControl
{
#region Public Section
public ObservableCollection<object> UCItems
{
get;
set;
}
#endregion
public myUserControl1()
{
InitializeComponent();
UCItems = new ObservableCollection<object>();
}
#region UCItemsSource Property
public static readonly DependencyProperty UCItemsSourceProperty =
DependencyProperty.Register("UCItemsSource",typeof(IEnumerable),typeof(myUserControl1));
public IEnumerable UCItemsSource
{
get { return (IEnumerable)GetValue(UCItemsSourceProperty); }
set { SetValue(UCItemsSourceProperty,value); }
}
#endregion
}
并将TexBox更改为xaml中的DataGrid:
<UserControl x:Class="OCasDPdemo.myUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OCasDPdemo"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<ItemsControl ItemsSource="{Binding Path=UCItemsSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding Path=UCItemsSource.Person}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我以与原始示例类似的方式填充集合:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Person> WindowCollection
{
get;
set;
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
var bob = new Person { FirstName = "Bob",LastName = "Brown",Age = 32.1,ID = 101 };
var jim = new Person { FirstName = "Jim",LastName = "Green",Age = 21.0,ID = 201 };
var mel = new Person { FirstName = "Mel",LastName = "Black",Age = 20,ID = 111 };
WindowCollection = new ObservableCollection<Person>() {bob,jim,mel };
}
private void Button_Click(object sender,RoutedEventArgs e)
{
var sue = new Person { FirstName = "Sue",LastName = "White",Age = 64.7,ID = 101 };
var ted = new Person { FirstName = "Ted",LastName = "Grey",Age = 18.3,ID = 191 };
WindowCollection.Add(sue);
WindowCollection.Add(ted);
}
}
MainWindow xaml是:
<Grid>
<StackPanel>
<local:myUserControl1 UCItemsSource="{Binding Path=WindowCollection}" />
<Button Content="Refresh" Click="Button_Click" />
</StackPanel>
</Grid>
我得到的是空行(与人数相同),而不是带有列的网格。此设置适用于本机类型,例如长型和字符串类型(使用TextBox)。有人可以让我知道我做错了吗。
解决方法
我自己还没有尝试过,但是问题似乎出在您的Future<void> main() async {
final d = await callApi();
final listTiles = <ListTile>[
...d.as.convertToListTiles(),...d.bs.convertToListTiles(),...d.cs.convertToListTiles(),];
//TODO: do something with `listTiles`
}
XAML中。
在根myUserControl1
内是Grid
,其中ItemsControl
绑定到ItemsSource
。这意味着UCItemsSource
将为该集合中的每个元素生成一个ItemsControl
-在您的情况下,将是您的ContentPresenter
s列表。
现在,将在每个Person
中创建一个ContentPresenter
的实例。您的DataTemplate
包含一个DataTemplate
。这意味着您将每DataGrid
获得一个完整的DataGrid
。
相反,如果您尝试使用一个Person
且每个DataGrid
行,那么您可能想要这样的东西:
Person
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。