如何在数据网格列中绑定我在代码中定义的形状?

时间:2017-03-13 16:30:50

标签: c# xaml wpfdatagrid

所以这是我的问题。我在xaml中构建了这个简单的接口

    <Window x:Class="WpfApplication2.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:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="302" Width="253.8">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel>
            <Label Target="{Binding ElementName=txtbxName}">_Cerca una lista</Label>
            <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" ToolTip="Inserisci il nome di una lista per filtrare l'elenco"/>
            <DataGrid CanUserAddRows="False" x:Name="dg"  ItemsSource="{Binding MyFilteredItems}" AutoGenerateColumns="False" HorizontalAlignment="Right" Margin="0,0,1.2,0" >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Width="*">
                        <DataGridTemplateColumn.Header >
                            <CheckBox HorizontalContentAlignment="Center" Checked="IsChecked" Unchecked="IsUncheck" ToolTip="Seleziona/Deseleziona tutto"/>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="c1" HorizontalAlignment="Stretch" IsChecked="{Binding Check}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Binding="{Binding Name}">
                        <DataGridTextColumn.Header>Name</DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Dimension}">
                        <DataGridTextColumn.Header>Dimensione</DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Colour}">
                        <DataGridTextColumn.Header>Colour</DataGridTextColumn.Header>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
        <Button HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10,0,0,35.4" Grid.Row="1" Width="60">Applica</Button>
        <Button HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10,0,0,35.4" Grid.Row="1" Width="60">Nascondi</Button>
        <Button HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10,0,0,35.4" Grid.Row="1" Width="60">Cancella</Button>
    </Grid>
</Window>

我的网格从类似的类的可观察集合中获取数据:

public class Righe :  INotifyPropertyChanged 
        {
            private bool check;

            public event PropertyChangedEventHandler PropertyChanged;

            public bool Check
            {
                get { return check; }
                set
                {
                    check = value;

                    OnPropertyChanged("Check");
                }
            }
            public string Name { get; set; }
            public string Dimension { get; set; }
            public Rectangle rect;
            public void OnPropertyChanged(string name)
            {
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

我想,而不是<DataGridTextColumn Binding="{Binding Colour}"> <DataGridTextColumn.Header>Colour</DataGridTextColumn.Header> </DataGridTextColumn>

一个列,在每个单元格中都有我在班级中定义的矩形形状。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

在我看来这种方法很糟糕,因为它不适合MVVM,但如果你知道你做了什么就可以使用DataGridTemplateColumn

       <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding rect}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>