【问题标题】:Change image.Source in ListView according to boolean根据布尔值更改 ListView 中的 image.Source
【发布时间】:2015-08-29 09:28:42
【问题描述】:

我有一个包含 2 列的列表视图:系统的“可见性”和“名称”。第 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_"我的图像名称",例如App.CONTROLLER.PATHS.PATH_TO_ADD;

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

非常感谢!

【问题讨论】:

    标签: c# wpf xaml listview boolean


    【解决方案1】:

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

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-13
      • 2014-12-30
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2020-08-03
      • 2017-10-10
      • 2021-03-12
      相关资源
      最近更新 更多