在ListView中动态更改图像的路径

时间:2013-03-08 13:29:17

标签: c# wpf listview

大家好我是WPF新手,我使用列表视图渲染为网格视图。我有一个图像列,我通过绑定路径来设置值。现在的问题是我需要根据条件改变图像的路径。如果条件为真,我需要图像的路径不同。如果为false,我将其更改为其他路径。这可以一行一行吗?以下是我的列表视图。请帮忙

<ListView Name="LstGrd" Margin="0,105,0,138">
    <ListView.View>
        <GridView  >
            <GridViewColumn Header=" Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />
            <GridViewColumn Header=" Address" Width="250" DisplayMemberBinding="{Binding Path=Address}" />
            <GridViewColumn Header=" City" Width="50" DisplayMemberBinding="{Binding Path=City}" />
            <GridViewColumn Header=" State" Width="75" DisplayMemberBinding="{Binding Path=State}" />
            <GridViewColumn Header=" PostalCode" Width="75" DisplayMemberBinding="{Binding Path=PostalCode}" />
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Name="Test" Height="16" Source="{Binding Path=ImagePath,Mode=TwoWay}"/>
                            <TextBlock Text="Status"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您希望在某些条件发生变化时更改图像。我创建了一个绑定到listview的类的虚拟版本,并添加了一个名为“IsSelected”的属性来显示它。

<ListView Name="LstGrd" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <ListView.View>
        <GridView  >
            <GridViewColumn Header="">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding IsSelected}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header=" Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />
            <GridViewColumn Header=" Address" Width="250" DisplayMemberBinding="{Binding Path=Address}" />
            <GridViewColumn Header=" City" Width="50" DisplayMemberBinding="{Binding Path=City}" />
            <GridViewColumn Header=" State" Width="75" DisplayMemberBinding="{Binding Path=State}" />
            <GridViewColumn Header=" PostalCode" Width="75" DisplayMemberBinding="{Binding Path=PostalCode}" />
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                        <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Name="Test" Height="16" Source="{Binding Path=ImagePath,Mode=TwoWay}"/>
                            <TextBlock Text="Status"/>
                        </StackPanel>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsChecked}" Value="True">
                                <Setter Property="Source" Value="{Binding Path=ImagePath2}" TargetName="Test" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

这里要注意的是我添加了一个列(这是可选的 - 我这样做是为了让它更容易演示)。然后,在“图片”列中,您需要向DataTrigger添加DataTemplate以处理该属性更改。

这里只是DataTemplate:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Name="Test" Height="16" Source="{Binding Path=ImagePath,Mode=TwoWay}"/>
         <TextBlock Text="Status"/>
    </StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsChecked}" Value="True">
            <Setter Property="Source" 
                    Value="{Binding Path=ImagePath2}" 
                    TargetName="Test" />
         </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

这比转换器更有优势,因为它只是一个XAML实现。

答案 1 :(得分:0)

你应该使用转换器:

<Image Source="{Binding Path = ImagePath, Mode = TwoWay, Converter={StaticResource imageToValidatedImageConv}}" />

其中imageToValidatedImageConv是定义为的资源:<local:ImageToValidatedImageConverter x:Key="imageToValidatedImageConv" />

和您的转换器类似:

public class ImageToValidatedImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if()
            {
                //return image1 
            }
            else
            {
                //return image2
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("Cant convert back");
        }
    }