C# 以编程方式访问WPF控件
这个问题可能很明显,但我很难看到 我有以下XAML:C# 以编程方式访问WPF控件,c#,wpf,.net-4.5,C#,Wpf,.net 4.5,这个问题可能很明显,但我很难看到 我有以下XAML: <ItemsControl x:Name="contentList"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> &l
<ItemsControl x:Name="contentList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" />
<ItemsControl x:Name="imageContent" Grid.Column="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ImageCollection.FullName}" TextWrapping="Wrap" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然而,当我尝试对imageContent执行相同操作时,我似乎无法通过IntelliSense访问它。
我尝试过对项目进行清理/重建,但没有任何效果
我是否需要以不同的方式访问图像内容
我希望对contentList和imageContent使用myObservableCollection,因为它具有以下结构:
- 名称(字符串)
- 图像采集(可观测采集)
您需要在外部集合的列表对象中定义另一个ObservableCollection。大概是这样的:
ObservableCollection<MyObject> OuterList = new ObservableCollection<MyObject>();
//...
public class MyObject
{
public ObservableCollection<FileInfo> ImageCollection {get; set;}
public MyObject()
{
ImageCollection = new ObservableCollection<FileInfo>();
}
}
ObservableCollection OuterList=新的ObservableCollection();
//...
公共类MyObject
{
公共ObservableCollection ImageCollection{get;set;}
公共对象()
{
ImageCollection=新的ObservableCollection();
}
}
然后只需更新您的xaml,如下所示:
...
<ItemsControl x:Name="imageContent" ItemsSource="{Binding ImageCollection}">
...
。。。
...
因此,这将导致外部列表中的每一项都保存有自己的可观察集合,保存着自己的列表
此外,通过此更改,请确保更新文本块上的绑定,因为每个项将表示一个FileInfo对象,您只需编写以下内容:
<DataTemplate>
<TextBlock Text="{Binding FullName}" TextWrapping="Wrap" />
</DataTemplate>
您的imageContent
位于外部控件的ItemTemplate内。当外部控件绑定到>=2个项目时,imageContent
的唯一性如何?我明白了,将有多个控件具有相同的名称。我是否试图错误地使用控件?我不知道如何命名它。但是您应该能够将它绑定到一个项目列表。像前面控件中的{Binding Name}
一样:{Binding List}
我尝试将文本块的绑定设置为{Binding Name},作为测试,没有额外的输出。输出窗口似乎也没有抱怨不正确的绑定。感谢KDiTraglia,但我想使用的ItemsSource是myObservableCollection对象,它本身有一个名为ImageCollection的属性,类型为ObservableCollection。这就是为什么我有ImageCollection.FullName的绑定。我已经用更多信息更新了我的问题,如果您有任何意见,我将不胜感激。@JamieKeeling听起来您只需要在xaml中使用这一行,ItemsSource=“{Binding ImageCollection}”
。只要确保它是一个属性(有一个getter和setter),如果您的数据结构直接反映了您所显示的内容,那么您的生活就会变得轻松愉快。您需要一个外部集合,其中每个项表示一行。此项将有两个属性:名称和FileInfo的列表。因此,您将拥有多个FileInfo集合:每个名称一个。单个巨大的FileInfo集合对于数据绑定几乎没有用处,因为它只能生成单个巨大的对象行。感谢@IvanKrivyakov根据您的建议修改了我的XAML,效果非常好。
<DataTemplate>
<TextBlock Text="{Binding FullName}" TextWrapping="Wrap" />
</DataTemplate>