通过xaml

时间:2017-11-10 15:13:46

标签: wpf xaml mvvm

我尝试设置Datagrid,以便在RowHeader中显示一个图像,以显示列出的每个对象的状态。

为了达到这个目的,我的视图模型中有一个IsTaskCompletedSuccessfully可以为空的布尔属性。

在我的资源中,我添加了一个具有以下样式的图像:

            <DataGrid.Resources>
                <Image x:Name="editImage"
                       x:Key="rowHeaderTemplate"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Width="16"
                       Margin="1,0">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Style.Triggers>
                                <DataTrigger  Binding="{Binding IsTaskCompletedSuccessfully}" Value="true">
                                    <Setter Property="Source" Value="/RFiDGear;component/Resources/confirm.ico"/>
                                </DataTrigger >
                                <DataTrigger  Binding="{Binding IsTaskCompletedSuccessfully}" Value="false">
                                    <Setter Property="Source" Value="/RFiDGear;component/Resources/error.ico"/>
                                </DataTrigger >
                                <DataTrigger  Binding="{Binding IsTaskCompletedSuccessfully}" Value="{x:Null}">
                                    <Setter Property="Source" Value="/RFiDGear;component/Resources/wait.ico"/>
                                </DataTrigger >
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
            </DataGrid.Resources>

下面我有rowheaderstyle:

            <DataGrid.RowHeaderStyle>
                <Style TargetType="{x:Type DataGridRowHeader}">
                    <Setter Property="Content" Value="{StaticResource rowHeaderTemplate}"/>
                </Style>
            </DataGrid.RowHeaderStyle>

不幸的是,它只能在最后一行/对象上添加,而不是在所有行上添加。我在这里想念的是什么?!

EDIT1:

我将Style编辑为RowHeaderTemplate。后来我发现数据绑定这种方式不正常。我的图片来源不会更新。运行时输出窗口中没有绑定错误。

        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <Image x:Name="editImage"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Width="16"
                       Margin="1,0">
                </Image>

                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsTaskCompletedSuccessfully}" Value="True">
                        <Setter TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/confirm.ico"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding IsTaskCompletedSuccessfully}" Value="False">
                        <Setter  TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/error.ico"/>
                    </DataTrigger>

                    <!--<DataTrigger Binding="{Binding IsTaskCompletedSuccessfully}" Value="{x:Null}">
                        <Setter TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/wait.ico"/>
                    </DataTrigger>-->

                </DataTemplate.Triggers>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>

1 个答案:

答案 0 :(得分:0)

搞定了。我必须使用relativesource来查找datacontext。

            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <Image x:Name="editImage"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Width="16"
                           Margin="1,0">
                    </Image>

                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=DataContext.IsTaskCompletedSuccessfully}" Value="True">
                            <Setter TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/confirm.ico"/>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=DataContext.IsTaskCompletedSuccessfully}" Value="False">
                            <Setter  TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/error.ico"/>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=DataContext.IsTaskCompletedSuccessfully}" Value="{x:Null}">
                            <Setter TargetName="editImage" Property="Source" Value="/RFiDGear;component/Resources/wait.ico"/>
                        </DataTrigger>

                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>