Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中修改listbox的数据模板属性_C#_Wpf_Xaml_Datatemplate_Code Behind - Fatal编程技术网

C# 在代码隐藏中修改listbox的数据模板属性

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"/> &

我有一个XAML中的列表框,代码如下所示

<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;
}