我有一个包含2列的列表视图:系统的“可见性”和“名称”。第1列的值:带图像的按钮(btn_visibility)(img_visibility)。
根据来自代码后面的对象的布尔值(true或false),图像应该改变,例如SYSTEM.SHOW_IN_LIST = true; 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>
非常感谢提前!
答案 0 :(得分:2)
对于那些正在寻找解决方案的人:
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
}
}
编辑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>
只需在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 };
希望这有帮助。