繁体   English   中英

WPF DataGrid RowStyle如何使用图像或画笔作为背景

[英]Wpf DataGrid RowStyle How use either Image or Brush for Background

我在针对DataGrid的样式中的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>

某些DataGrid将需要一个RowStyle,它将一个图像用作背景,以在某个属性为true时显示。
我修改了多重绑定以将datacontext作为第三个值传递,并修改了转换器以检查DC以确定是否需要图像。
不过,我不知道如何构造xaml。
更新:
这似乎可行,除了设置TwoWay绑定外,我需要指定一个路径。

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

如何设置绑定路径以绑定到DC(等效于<Binding /> )? 上面的xaml中的绑定给了我必要的DC。 如何使用路径表达同一件事?
感谢您的见解。

同一属性,您不能有超过1个setter。 您可以根据样式使用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>

暂无
暂无

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

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