WPF DataGrid CellStyle似乎只在设计模式下工作?

时间:2015-01-02 23:42:28

标签: c# wpf datagrid

当我创建此DataGrid并尝试应用CellStyle时,它可以在Designer中运行,但在运行时似乎不应用该样式。我该如何解决这个问题?

<Window x:Class="ToyApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type DataGridCell}" x:Key="ButtonAdorners">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Red" SnapsToDevicePixels="True">
                            <Button DockPanel.Dock="Left" Margin="1" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">-</Button>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid DataContext="{DynamicResource SampleDataSource}">
        <DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Card Name" Binding="{Binding Property1}" CellStyle="{DynamicResource ButtonAdorners}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

0 个答案:

没有答案