2016-09-13 58 views
0

我目前有:试图绑定列标题的属性在我的C#代码

<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> 

我收到的错误是“属性'内容'只能设置一次”。

任何帮助,将不胜感激!

回答

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>