C# 在代码隐藏中修改listbox的数据模板属性
我有一个XAML中的列表框,代码如下所示C# 在代码隐藏中修改listbox的数据模板属性,c#,wpf,xaml,datatemplate,code-behind,C#,Wpf,Xaml,Datatemplate,Code Behind,我有一个XAML中的列表框,代码如下所示 <ListBox name="myListBox"> <ListBox.ItemTemplate> <DataTemplate> <Image Source="{Binding Path=Image}" Width="175" Height="175" VerticalAlignment="Center" HorizontalAlignment="Center"/> &
<ListBox name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image}" Width="175" Height="175" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在运行时,根据条件,我希望使用代码隐藏将高度
和宽度
属性更改为另一个值。请有人指导我实现所需的功能
非常感谢我认为实现这一点的最简单方法可能是将图像的宽度和高度绑定到两个属性。如果要更改所有图像的宽度和高度,可以在“代码隐藏”中使用两个属性;如果希望能够单独更改,则只需执行相同的操作,但绑定集合项中的属性即可
<ListBox name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image}"
Width="{Binding ElementName=myWindow,
Path=ListBoxTemplateWidth}"
Height="{Binding ElementName=myWindow,
Path=ListBoxTemplateHeight}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这样,ListBoxItems的大小将随图像的宽度/高度而增加/减少。listbox或ListBoxItem的高度和宽度是多少?
private double m_listBoxTemplateHeight;
public double ListBoxTemplateHeight
{
get
{
return m_listBoxTemplateHeight;
}
private set
{
m_listBoxTemplateHeight = value;
OnPropertyChanged("ListBoxTemplateHeight");
}
}
private double m_listBoxTemplateWidth;
public double ListBoxTemplateWidth
{
get
{
return m_listBoxTemplateWidth;
}
private set
{
m_listBoxTemplateWidth = value;
OnPropertyChanged("ListBoxTemplateWidth");
}
}
if (someCondition == true)
{
ListBoxTemplateHeight = 200;
ListBoxTemplateWidth = 200;
}