尝试将列标题绑定到我的C#代码中的属性

时间:2016-09-13 20:38:12

标签: xaml

我目前有:

<Window x:Class="Client_SCM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Client_SCM"
    mc:Ignorable="d"
    Title="Swords Call Monitor 2.0" Height="350" Width="474">

<Grid HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      Margin="5"
      ShowGridLines="True">
    <DataGrid x:Name="dataGrid"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Margin="5"
              AlternatingRowBackground="Aqua" Loaded="dataGrid_Loaded" AutoGenerateColumns="False"/>

</Grid>

我正试图在其中实现这样的事情:

<DataGridTextColumn Binding="{Binding WhateverIWantToDisplay}" >

    

  <Setter Property="Background" Value="Green" />

  <Style.Triggers>
    <DataTrigger Binding="{Binding Foo}" Value="1">
      <Setter Property="Background" Value="Blue" />
    </DataTrigger>

    <DataTrigger Binding="{Binding Foo}" Value="2">
      <Setter Property="Background" Value="Red" />
    </DataTrigger>

    <DataTrigger Binding="{Binding Foo}" Value="2">
      <Setter Property="Background" Value="Yellow" />
    </DataTrigger>

  </Style.Triggers>
</Style>

我得到的错误是&#34;属性&#39;内容&#39;只能设置一次&#34;。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

尝试在Header上使用Binding代替DataGridTextColumn

<Window x:Class="Client_SCM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Client_SCM"
    mc:Ignorable="d"
    Title="Swords Call Monitor 2.0" Height="350" Width="474">

    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ShowGridLines="True">
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"
                  AlternatingRowBackground="Aqua" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="{Binding WhateverIWantToDisplay}">
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>