简体   繁体   English

WPF数据绑定和图像

[英]WPF Data binding and Image

I have this Xaml code 我有这个Xaml代码

<ListView x:Name="listOrderList" Margin="58,55,0,0" Background="{x:Null}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="DESCRIPTION" DisplayMemberBinding="{Binding Description}"/>
            <GridViewColumn Header="STATUS">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Status}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

In C# I have an enum for Status and i know obviously <Image Source="{Binding Status}"/> is not going to work. 在C#中,我有一个Status的enum ,而且我显然知道<Image Source="{Binding Status}"/>无法正常工作。 But how can do something for example i have two values in Status enum. 但是如何做某事,例如我在Status枚举中有两个值。 Say Yes and No. How can i assign small icons to enum's values and work with Bindings easily. 说“是”和“否”。我如何为枚举的值分配小图标,并轻松使用Bindings。

Thanks you. 谢谢。

Check out IValueConverter 看看IValueConverter

With that you can check which value was delivered by the binding and then create the Image object to display. 这样,您可以检查绑定传递了哪个值,然后创建要显示的Image对象。

Either you use a binding converter, or you add DataTriggers to your DataTemplate. 您可以使用绑定转换器,也可以将DataTriggers添加到DataTemplate中。 The example below assumes the following enum 下面的示例假定以下枚举

public enum Status { Status1, Status2 }

and sets the Image's Source property to different image resources depending on the value of the Status property. 并根据Status属性的值将Image的Source属性设置为其他图像资源。

<Window.Resources>
    <BitmapImage x:Key="Image1" UriSource="..."/>
    <BitmapImage x:Key="Image2" UriSource="..."/>
</Window.Resources>

...

<DataTemplate>
    <Image x:Name="image"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="Status1">
            <Setter TargetName="image" Property="Source"
                    Value="{StaticResource Image1}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Status}" Value="Status2">
            <Setter TargetName="image" Property="Source"
                    Value="{StaticResource Image2}"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

You can set Icon image path to Enum, then Bind DataGrid with that Enum. 您可以将Icon图像路径设置为Enum,然后将DataGrid与该Enum绑定。

            <DataGridTemplateColumn x:Name="colStatus" Width="160" Header="Status">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                   <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" OverridesDefaultStyle="True" Width="135">
                                    <Image Height="14" Margin="2,0,0,0" HorizontalAlignment="Left" Source="{Binding Status}" Width="14" />
                                    </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM