我想将一个Image添加到ListBox Item。
我有2张图片。一个是UpArrow,第二个是DownArrow
我可以使用ItemTemplate指定初始图像说UpArrow并向其添加图像
但是点击排序按钮,我想更改图像。新图像已设置但未在UI上更改。
我的窗口代码如下
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="Auto" Width="Auto" Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ListBox x:Name="lstBox" ItemsSource="{Binding ListIconDataCollection}" Grid.Row="0" Height="200" Width="200">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:ListIconData}">
<Grid Width="Auto" Height="Auto" Background="Transparent" >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" Text="{Binding DisplayText}"/>
<Image Grid.Row="0" Grid.Column="1" Source="{Binding Path=ImageSource}" Width="20" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnSort" Grid.Row="1" Height="40" Margin="0,15,0,0" Content="Sort" Click="btnSort_Click"></Button>
</Grid>
我的表单代码位于
之下private void Window_Loaded(object sender, RoutedEventArgs e)
{
lstDataToSet.Add(new ListIconData { DisplayText = "Milind", ItemSortDirection = SortDirection.None, ImageSource = (ImageSource)FindResource("ImgUp") });
lstDataToSet.Add(new ListIconData { DisplayText = "Patil", ItemSortDirection = SortDirection.None });
lstBox.ItemsSource = ListIconDataCollection;
}
这是执行的表单代码,但图片在UI上没有变化
private void btnSort_Click(object sender, RoutedEventArgs e)
{
ListIconData objSelectedItem = (ListIconData)lstBox.SelectedItem;
if (objSelectedItem.ItemSortDirection==SortDirection.None)
{
objSelectedItem.ImageSource = null;
objSelectedItem.ImageSource = (ImageSource)FindResource("ImgDown");
}
}
这是Dictionary1资源文件代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="ImgUp">icons/Up.ico</ImageSource>
<ImageSource x:Key="ImgDown">icons/Down.ico</ImageSource>
以下是ListIconDataClass。这是我绑定到ListBox的列表
public class ListIconData
{
public string DisplayText { get; set; }
public SortDirection ItemSortDirection { get; set; }
public ImageSource ImageSource { get; set; }
}
}
答案 0 :(得分:0)
正如nikeee13建议您应该发送有关模型类中的更改的通知
public class ListIconData : INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
public string DisplayText { get; set; }
public SortDirection ItemSortDirection { get; set; }
private ImageSource imgSource;
public ImageSource ImageSource {
get{return imgSource;}
set{
if(imgSource == null || !imgSource.Equals(value))
{
imgSource = value;
OnPropertyChanged("ImageSource");
}
}
}
}
答案 1 :(得分:0)
private void btnSort_Click(object sender, RoutedEventArgs e)
{
**lstBox.BeginInit();**
ListIconData objSelectedItem = (ListIconData)lstBox.SelectedItem;
if (objSelectedItem.ItemSortDirection==SortDirection.None)
{
objSelectedItem.ImageSource = null;
objSelectedItem.ImageSource = (ImageSource)FindResource("ImgDown");
}
**lstBox.EndInit();**
}
请找到我添加的粗体中的代码行,以使其正常工作 谢谢大家的帮助