这是我的 xaml 的一部分:
<DataGrid ItemsSource="{Binding listImages, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" Name="dataGrid1" Height="341" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,10,0" Width="225" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserAddRows="False" IsHitTestVisible="True" OverridesDefaultStyle="False" SelectedIndex="0" SelectionUnit="FullRow" SelectionMode="Single">
<DataGrid.InputBindings>
<KeyBinding Key="a" Command="{Binding Path=KeyPressed}"/>
<KeyBinding Key="s" Command="{Binding Path=KeyPressed}"/>
<KeyBinding Key="d" Command="{Binding Path=KeyPressed}"/>
</DataGrid.InputBindings>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding rowColor}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="ImageName" Binding="{Binding imageName}" Width="112" />
<DataGridTextColumn Header="Tag" Binding="{Binding tag}" Width="110" />
</DataGrid.Columns>
我想加载一个 .xaml ,其中只定义了这个 datagrid 或者更好的是只定义了KeyBindings。这是因为用户可以更改可以绑定哪些键。
示例:Tom 想要使用键 z,x,c 而不是 a,s,d。为此,他编辑默认 xml/xaml(位于某处)并更改 KeyBinding 的Key参数并加载它。
新的 xml/xaml 将如下所示:
<DataGrid ItemsSource="{Binding listImages, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" Name="dataGrid1" Height="341" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,10,0" Width="225" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserAddRows="False" IsHitTestVisible="True" OverridesDefaultStyle="False" SelectedIndex="0" SelectionUnit="FullRow" SelectionMode="Single">
<DataGrid.InputBindings>
<KeyBinding Key="z" Command="{Binding Path=KeyPressed}"/>
<KeyBinding Key="x" Command="{Binding Path=KeyPressed}"/>
<KeyBinding Key="c" Command="{Binding Path=KeyPressed}"/>
</DataGrid.InputBindings>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding rowColor}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="ImageName" Binding="{Binding imageName}" Width="112" />
<DataGridTextColumn Header="Tag" Binding="{Binding tag}" Width="110" />
</DataGrid.Columns>
有可能遵循 MVVM 模式吗?