这是我的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,或者更好的只定义KeyBindings。这是因为用户可以更改可以绑定的键。
示例: Tom希望使用键z,x,c而不是a,s,d。为此,他编辑了默认的xml/xaml (位于某个地方),并更改了键参数KeyBinding并加载它。
新的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模式执行此操作?
发布于 2012-09-05 11:18:46
我不会将用户的键绑定选择保留为XAML。Binding标签等对用户来说没有任何意义。
相反,以其他更友好的方式持久化键绑定,甚至可能是一个简单的ini样式文本文件,例如:
Command1Key=z
Command2Key=x
Command3Key=c一旦将它们加载到ViewModel中,就可以将它们作为InputBindingCollection属性公开到视图中,然后视图可以绑定到视图中。
https://stackoverflow.com/questions/12280106
复制相似问题