首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >加载xml/xaml并定义键绑定

加载xml/xaml并定义键绑定
EN

Stack Overflow用户
提问于 2012-09-05 11:08:37
回答 1查看 280关注 0票数 0

这是我的xaml的一部分:

代码语言:javascript
运行
复制
<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将如下所示:

代码语言:javascript
运行
复制
<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模式执行此操作?

EN

Stack Overflow用户

回答已采纳

发布于 2012-09-05 11:18:46

我不会将用户的键绑定选择保留为XAML。Binding标签等对用户来说没有任何意义。

相反,以其他更友好的方式持久化键绑定,甚至可能是一个简单的ini样式文本文件,例如:

代码语言:javascript
运行
复制
Command1Key=z
Command2Key=x
Command3Key=c

一旦将它们加载到ViewModel中,就可以将它们作为InputBindingCollection属性公开到视图中,然后视图可以绑定到视图中。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12280106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档