2016-09-13 38 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> 
相關問題