首页 文章

Wpf DataGrid RowStyle如何使用Image或Brush for Background

提问于
浏览
0

我在针对DataGrid的Style中的xaml中定义了RowStyle:

<Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}">
<!-- Bunch of other setters -->
<Setter Property="RowStyle">
    <Setter.Value>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource DataGridRowBgConverter}">
                        <Binding Path="IsThis" />
                        <Binding Path="IsThat" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </Setter.Value>
</Setter>

某些DataGrids需要一个RowStyle,它使用一个图像作为背景,当某个属性为true时显示 .
我修改了multibinding以传递datacontext作为第三个值,并修改了转换器以检查DC以确定是否需要图像 .
但是,我无法弄清楚如何构建xaml .
更新:
这似乎工作,除了设置TwoWay绑定,我需要指定一个路径 .

<Setter Property="Background">
      <Setter.Value>
        <ImageBrush Binding="{Binding Converter={StaticResource DataGridRowImageConverter}}"  />
      </Setter.Value>
  </Setter>

绑定路径如何设置为绑定到DC(相当于 <Binding /> )?上面的xaml中的绑定给了我必要的DC;如何使用Path表达相同的内容?
感谢您的任何见解......

1 回答

  • 0

    你不能拥有超过1个同一 property 的二举手 . 您可以使用样式上的DataTrigger根据DataContext中的属性将背景设置为ImageBrush . 它看起来像下面这样:

    <Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}">
        <!-- Bunch of other setters -->
        <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource DataGridRowBgConverter}">
                            <Binding Path="IsThis" />
                            <Binding Path="IsThat" />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=PropertyToIndicateImageBackground}" Value="True">
                <Setter Property="Background">
                    <ImageBrush ImageSource="YourImage.jpg"/> 
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

相关问题