根据boolean改变ListView中的image.Source

Change image.Source in ListView according to boolean

我有一个包含 2 列的列表视图:系统的 "visibility" 和 "name"。第 1 列的值:带有图像 (img_visibility) 的按钮 (btn_visibility)。

根据图像背后代码中对象的布尔值(真或假),图像应该改变,例如SYSTEM.SHOW_IN_LIST = 真; img_visibility.Source = new BitmapImage(new Uri(App.CONTROLLER.PATHS.PATH_TO_MINUS));

可以在以下相对路径下访问图像:App.CONTROLLER.PATHS.PATH_TO_"Name of my image",例如App.CONTROLLER.PATHS.PATH_TO_添加;

我的 xaml-代码在 SystemmanagementWindow.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>

非常感谢!

对于那些正在寻找解决方案的人:

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

在此处找到: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

已编辑 MainWindow.xaml 以与多列列表视图并行工作:

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

只需将以下行添加到 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 };

希望这对您有所帮助。