C# 为索引属性创建模板
我有一个视图模型,其中包含一组C# 为索引属性创建模板,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我有一个视图模型,其中包含一组项属性,每个属性又包含一个图像路径。可能存在0。。N集合中的项目属性 我的视图中的stackpanel包含三个相同的图像控件。每个图像控件都绑定到项目属性图像的路径: 图像控件1绑定到第1项属性的图像路径(在ItemAttributes[0]中找到)。图像 图像控件2绑定到第二项属性的图像路径(在ItemAttributes.image中找到) 图像控件3绑定到第三项属性的图像路径(在ItemAttributes[2]中找到)。图像 如果有3个以上的属性,则忽略
项属性
,每个属性又包含一个图像路径。可能存在0。。N
集合中的项目属性
我的视图中的stackpanel包含三个相同的图像控件。每个图像控件都绑定到项目属性图像的路径:
- 图像控件1绑定到第1项属性的图像路径(在ItemAttributes[0]中找到)。图像
- 图像控件2绑定到第二项属性的图像路径(在ItemAttributes.image中找到)
- 图像控件3绑定到第三项属性的图像路径(在ItemAttributes[2]中找到)。图像
<DataTrigger Binding="{Binding Attribute1}" Value="{x:Null}">
<Setter Property="Source" Value="{x:Null}"/>
</DataTrigger>
所以我的问题是,我想让这个数据触发器为所有三个图像属性和相应的图像控件(以及一些其他属性,如宽度、高度、边距等)工作。所以我认为,把它放在一个样式中,并将其作为静态资源引用。当我有三个名称不同的属性(Attribute1、Attribute2、Attribute3)时,这将不起作用。所以现在我只能这样做:
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding Attribute1}" />
<Setter Property="Width" Value="44" />
<Setter Property="Height" Value="45" />
<Setter Property="Margin" Value="5" />
<Style.Triggers>
<DataTrigger Binding="{Binding Attribute1}" Value="{x:Null}">
<Setter Property="Source" Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
对其他两个图像控件重复此操作,但Attribute1被Attribute2和Attribute3替换
因此,我想知道是否有一种方法可以绑定到集合,例如
<DataTrigger Binding="{Binding Attributes}" Value="{x:Null}">
<Setter Property="Source" Value="{x:Null}"/>
</DataTrigger>
。。。然后在模板之外指定我对图像控件绑定感兴趣的索引(我想类似于将参数传递给数据触发器)
任何想法。。。如果不可能,还有其他方法吗
<ItemsControl ItemsSource="{Binding Attributes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source={Binding Something} x:Name=Image/>
<DataTemplate.Triggers>
<DataTrigger Binding={Binding Something} Value={x:Null}>
<Setter TargetName=Image Property=Source Value={x:Null}/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
为了将其限制为前三个项属性,这是我在视图模型的构造函数中使用的。Attributes是一个SmartObservableCollection属性(自定义ObservableCollection with and AddRange方法和一些其他好东西),带有一个_Attributes支持字段:
_attributes = new SmartObservableCollection<string>();
var images = from attributes in _item.Attributes
select attributes.Image;
Attributes.AddRange(images.Take(3));
\u attributes=new SmartObservableCollection();
var images=来自_item.attributes中的属性
选择属性。图像;
Attributes.AddRange(images.Take(3));
老兄,你为什么要把事情复杂化这么多?只需使用绑定到某个可观察集合的ItemsControl
,然后忘记Attribute1、Attribute2、Attribute3…
@HighCore:我在WPF世界中还是很新手,所以我并不总是选择最简单或最合适的方法来做某事。谢谢。我会试试看。。。我还在学习,所以过度复杂化通常是因为我没有完全理解一些东西。+1是的,不会编译,因为它是免费的,但核心在那里。我在答案中的更新编译。它只是缺少了一些关于图像源绑定和一些数据触发器绑定的引号。
<ItemsControl ItemsSource="{Binding Attributes}"
Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Left" VerticalAlignment="Top" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image x:Name="Image"
Source="{Binding}"
Width="45" Height="44" Margin="5" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter TargetName="Image"
Property="Source" Value="{x:Null}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
_attributes = new SmartObservableCollection<string>();
var images = from attributes in _item.Attributes
select attributes.Image;
Attributes.AddRange(images.Take(3));