我有两个单元格模板,一个用于“ ReadOnly”,另一个用于“ CellEditing”。 按钮绑定了将变量(IsEditable)切换为true / false的命令。 当我单击按钮时,直到双击单元格,样式才会改变。
我的问题是,如何动态更改DataGrid单元格样式而无需双击该单元格。
我的XAML:
<DataTemplate x:Key="ReadOnlyTemplate">
<TextBox Text="{Binding ProductionName}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
<DataTemplate x:Key="CellEditingTemplate">
<TextBox Text="{Binding ProductionName}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
...
<DataGridTemplateColumn Header="Name" CellTemplate="{StaticResource ReadOnlyTemplate}" CellEditingTemplate="{StaticResource CellEditingTemplate}"/>
答案 0 :(得分:0)
CellEditingTemplate仅在您已经编辑单元格时才应用。您想要的是在不编辑时修改样式。 试试这个:
<groupId>com.github.maven-nar</groupId>
<artifactId>maven-nar-poc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>nar</packaging>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.2.3</version>
<extensions>true</extensions>
<configuration>
<libraries>
<library>
<type>jni</type>
<narSystemPackage>com.github.maven-nar</narSystemPackage>
</library>
</libraries>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/nar/nar-generated/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<skip>false</skip>
<cpp>
<debug>true</debug>
<includePaths>
<includePath>${project.basedir}/src/main/cpp</includePath>
</includePaths>
<options>
<option>-DMAC -DMAC_OSX -DMAC_CARBON -D__CF_USE_FRAMEWORK_INCLUDES__ -DLARGE64_FILES
</option>
</options>
</cpp>
<libraries>
<library>
<type>executable</type>
<!-- <run>true</run> -->
<subSystem>gui</subSystem>
</library>
</libraries>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<skip>false</skip>
<cpp>
<debug>true</debug>
<includePaths>
<includePath>${project.basedir}/src/main/cpp</includePath>
</includePaths>
<options>
<option>-DWINDOWS -D__CF_USE_FRAMEWORK_INCLUDES__ -DLARGE64_FILES
</option>
</options>
</cpp>
<libraries>
<library>
<type>executable</type>
<!-- <run>true</run> -->
<subSystem>gui</subSystem>
</library>
</libraries>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
并将其用作CellTemplate。
<DataTemplate x:Key="ProductionNameTemplate" >
<TextBox Text="{Binding ProductionName}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>