我写了一个自定义datarid控件,但是数据绑不上去,然后我把这段话注释了之后数据就可以显示了,但是样式就没有了,加上之后数据就绑不上去了

解决方案 »

  1.   

    以下是样式
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfApplication1">
        <Style TargetType="{x:Type local:KDataGrids}" BasedOn="{x:Null}">
            <Setter Property="RowHeaderWidth" Value="0"></Setter>
            <Setter Property="AutoGenerateColumns" Value="False"></Setter>
            <Setter Property="CanUserAddRows" Value="False"></Setter>
            <Setter Property="CanUserResizeColumns" Value="False"></Setter>
            <Setter Property="CanUserResizeRows" Value="False"></Setter>
            <Setter Property="HorizontalGridLinesBrush" Value="LightGray"></Setter>
            <Setter Property="VerticalGridLinesBrush" Value="LightGray"></Setter>
            <Setter Property="IsReadOnly" Value="True"></Setter>
            <Setter Property="BorderThickness" Value="1,0"></Setter>
            <Setter Property="BorderBrush" Value="LightGray"></Setter>
            <Setter Property="RowHeight" Value="30"></Setter>
            <Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <DataGridColumnHeader FontFamily="微软雅黑" Background="Red" FontSize="13"  Foreground="#0d2d4f"   HorizontalAlignment="Center" VerticalAlignment="Top" BorderBrush="Gray" BorderThickness="1,1,1,1" />
                            <DataGridCell Height="30">
                                <Grid>
                                    <StackPanel>
                                        <Border BorderBrush="Gray" BorderThickness="0.5,0.5,0,0.5"  >
                                            <TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="微软雅黑" />
                                        </Border>
                                    </StackPanel>
                                </Grid>
                                <DataGridCell.Style>
                                    <Style TargetType="DataGridCell">
                                        <Style.Triggers>
                                            <Trigger Property="IsSelected" Value="true">
                                                <Setter Property="Control.Background" Value="#FFC7CBCA"/>
                                                <Setter Property="Control.BorderThickness" Value="0"></Setter>
                                                <Setter Property="Control.Foreground" Value="Red"></Setter>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGridCell.Style>
                            </DataGridCell>
                            <ContentPresenter />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
      

  2.   

    这是generic里的
    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">    <ResourceDictionary.MergedDictionaries>
            
            <ResourceDictionary Source="/WpfApplication1;component/Themes/KDataGrids.xaml" />    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>标红的部分就是我引用的样式
      

  3.   

    打开一个DataGrid, 然后右键创建样式副本,看看DataGrid默认的样式;然后再写自己的样式;