Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF DataGrid CellStyle是否仅在设计模式下工作?_C#_Wpf_Datagrid - Fatal编程技术网

C# WPF DataGrid CellStyle是否仅在设计模式下工作?

C# WPF DataGrid CellStyle是否仅在设计模式下工作?,c#,wpf,datagrid,C#,Wpf,Datagrid,当我创建这个DataGrid并尝试应用CellStyle时,它在设计器中工作,但在运行时似乎没有应用样式。我该如何解决这个问题 <Window x:Class="ToyApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

当我创建这个DataGrid并尝试应用CellStyle时,它在设计器中工作,但在运行时似乎没有应用样式。我该如何解决这个问题

<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>

-

要么从样式中删除x:Key,要么将样式绑定到DataGrid的CellStyle属性。我想如果删除x:Key,它将应用于每个单元格,而不仅仅是列,对吗?绑定是什么样子的?在对它进行更多的工作之后,如果我将绑定从DynamicResource更改为StaticResource,它似乎在这两种情况下都能正常工作。为什么DynamicSource只能在设计模式下正常工作?