【问题标题】:Wpf DataGrid RowStyle How use either Image or Brush for BackgroundWpf DataGrid RowStyle 如何使用图像或画笔作为背景
【发布时间】:2016-07-27 10:15:13
【问题描述】:

我在 xaml 中以 DataGrid 为目标的样式中定义了一个 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 时显示。
我修改了多重绑定以将数据上下文作为第三个值传递,并修改了转换器以检查 DC 以确定是否需要图像。
不过,我不知道如何构建 xaml。
更新:
这似乎工作,除了设置双向绑定,我需要指定一个路径。

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

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

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    同一属性不能有超过 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>
    

    【讨论】:

    • 感谢您的回复。在创建数据网格之前,我们不知道将触发使用图像的属性名称,也不知道 imageSource。在您的示例中,DataTrigger 可以绑定到 DC 吗?我会尝试一下...
    • 您应该能够绑定到 DC。然后,您需要一个转换器在 DataTrigger 的绑定上返回 true/false
    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多