绑定到gridviewdatacolumn中的形状

时间:2015-02-02 21:56:19

标签: wpf xaml telerik

请原谅我的无知...... 我想让一个列在每个单元格中有一个小矩形,它根据DB中的值改变颜色。我现在已经让它工作,以便细胞背景改变颜色:

<telerik:GridViewDataColumn.CellStyle>
    <Style TargetType="telerik:GridViewCell">                                            
        <Style.Triggers>
                        <DataTrigger Binding="{Binding NewEmployee}" Value="True">                   
                <Setter Property="Background" Value="Yellow"/>                                                       
            </DataTrigger>                                                  
        </Style.Triggers>                                                                                                      
</telerik:GridViewDataColumn.CellStyle>
<telerik:GridViewDataColumn.CellTemplate>
    <DataTemplate>

        <!--HERE have <Rectangle Width="10" Height="10"/>-->   

    </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>  

谢谢

1 个答案:

答案 0 :(得分:0)

CS:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public List<MyItem> Items
    {
        get { return new List<MyItem> { new MyItem { Value = 1, IsEmpty = true }, new MyItem { Value = 2, IsEmpty = false } }; }
    }
}

public class MyItem
{
    public int Value { get; set; }
    public bool IsEmpty { get; set; }
}

XAML:

          <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding Items}">                     
        <DataGrid.Columns>
            <DataGridTemplateColumn >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <Rectangle  x:Name="rect" Fill="Green" Stretch="Uniform"  />
                            <ContentPresenter Grid.Column="1" Content="{Binding Value}"/>

                        </Grid>                               

                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsEmpty}" Value="True">
                                <Setter TargetName="rect" Property="Fill" Value="Red" />
                            </DataTrigger>
                        </DataTemplate.Triggers>

                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>