[英]Change image.Source in ListView according to boolean
I have a listview with 2 columns: "visibility" and "name" of a system. 我有一个包含2列的listview:系统的“可见性”和“名称”。 The value of column 1: a button (btn_visibility) with an image (img_visibility).
第1列的值:带有图像(img_visibility)的按钮(btn_visibility)。
Depending on a boolean (true or false) from an object in code behind the image should change, eg SYSTEM.SHOW_IN_LIST = true; 根据图像后面代码中对象的布尔值(真或假),应该更改,例如SYSTEM.SHOW_IN_LIST = true; img_visibility.Source = new BitmapImage(new Uri(App.CONTROLLER.PATHS.PATH_TO_MINUS));
img_visibility.Source =新的BitmapImage(新的Uri(App.CONTROLLER.PATHS.PATH_TO_MINUS));
An image is reachable under following relative path: App.CONTROLLER.PATHS.PATH_TO_"Name of my image", eg App.CONTROLLER.PATHS.PATH_TO_ADD; 在以下相对路径下可以访问图像:App.CONTROLLER.PATHS.PATH_TO_“我的图像名称”,例如App.CONTROLLER.PATHS.PATH_TO_ADD;
My xaml-code in SystemmanagementWindow.xaml: 我在SystemmanagementWindow.xaml中的xaml代码:
<ListView x:Name="lv_Systems" HorizontalAlignment="Left" Height="227" Margin="313,5,0,0" VerticalAlignment="Top" Width="153" SelectedIndex="0" SelectionChanged="listView_Systems_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="{x:Static p:Resources.SystemmanagementWindow_listView_col_1}" DisplayMemberBinding="{Binding SYSTEMNAME}" Width="110"/>
<GridViewColumn Header="{x:Static p:Resources.SystemmanagementWindow_listView_col_2}" Width="34">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Click="EditVisibility" CommandParameter="{Binding}" Width="20">
<Image x:Name="img_visibility" width="10" Height="10"/>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Many thanks in advance! 提前谢谢了!
For those who are searching for the solution: 对于那些正在寻找解决方案的人:
BoolToImageConverter.cs: BoolToImageConverter.cs:
public class BoolToImageConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? "D:\\Test\\Test\\bin\\Debug\\img\\add.png" : "D:\\Test\\Test\\bin\\Debug\\img\\minus.png";
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return false; // not needed
}
}
Found here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/b0be201f-cd9a-4cde-b3f8-35014c4ca485/wpf-how-to-show-some-items-and-hide-some-items-in-a-listview-base-on-a-bool-value?forum=wpf 在这里找到: https : //social.msdn.microsoft.com/Forums/vstudio/en-US/b0be201f-cd9a-4cde-b3f8-35014c4ca485/wpf-how-to-show-some-items-and-hide-some -items-IN-A-列表视图个碱基上-A-布尔值?论坛= WPF
Edited MainWindow.xaml for working parallel with multicolumn listview: 编辑了MainWindow.xaml以与多列listview并行工作:
<Window.Resources>
<local:BoolToImageConverter x:Key="image_converter"/>
<local:BoolToTooltipConverter x:Key="tooltip_converter"/>
</Window.Resources>
[...]
<ListView x:Name="listView" ItemsSource="{Binding List}">
<ListView.View>
<GridView>
<GridViewColumn Header="System" DisplayMemberBinding="{Binding Name}" Width="100"/>
<GridViewColumn Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button ToolTip="{Binding IsActive, Converter={StaticResource tooltip_converter}}">
<Image Source="{Binding IsActive, Converter={StaticResource image_converter}}" Width="15"/>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Just add following lines to MainWindow.cs to see the result: 只需将以下行添加到MainWindow.cs即可查看结果:
var list = new List<object>();
list.Add(new { Name = "First Name", IsActive = true });
list.Add(new { Name = "Second Name", IsActive = true });
list.Add(new { Name = "Third Name", IsActive = false });
list.Add(new { Name = "Fourth Name", IsActive = true });
list.Add(new { Name = "Fifth Name", IsActive = false });
list.Add(new { Name = "Sixth Name", IsActive = true });
list.Add(new { Name = "Seventh Name", IsActive = true });
DataContext = new { List = list };
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.