2014-02-13 93 views
0

所以我试图改变一些XAML代码来添加一个上下文菜单,它将改变一个值的小数位数。我的XAML虽然有点弱,但我有点失落。XAML中的动态绑定

我现在所拥有的代码是:

<MenuItem Header="{DynamicResource DecimalPlaces}" ItemsSource="{Binding MenuItems}"> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="{DynamicResource oneDecimal}" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="{DynamicResource twoDecimal}" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
</MenuItem> 

这将至少使画面出现,但问题是小数位数处理整数(我只是把oneDecimal和twoDecimal作为占位符现在),我想动态资源是一个整数,最好也从一个到十个。

所以我的问题是:我如何将动态资源设置为整数而不是特定变量,并且是否有一种方法可以动态生成此菜单(而不是写入10个不同的条目),可能基于数组或什么?

对不起,如果这是一个非常简单的问题,就像我说的,我的XAML有点弱。任何帮助不胜感激。

+0

正如答案所述,这里提出的问题并不清楚。你究竟想要做什么? – BradleyDotNET

回答

1

如果我正确理解你的问题,我不认为你需要一个DynamicResource。 DynamicResource是将在运行时解决的资源。这通常用于主题。

确切地理解你想要做什么有点困难,但是如果你只是希望头部显示一些文本,只需设置它。

<MenuItem Header="{DynamicResource DecimalPlaces}" ItemsSource="{Binding MenuItems}"> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="1" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="2" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="OneDecimal" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
    <ExclusiveMenuItem:ExclusiveMenuItem Header="TwoDecimal" IsCheckable="True" IsChecked="{Binding Path=DecimalPlaces}"/> 
</MenuItem> 

如果它需要一些来自MenuItems的数据,然后使用ItemTemplate或ItemContainerStyle。

<MenuItem Header="{DynamicResource DecimalPlaces}" ItemsSource="{Binding MenuItems}"> 
    <MenuItem.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="Header" Value="{Binding SomeProperty}" /> 
      <Setter Property="IsCheckable" Value="True" /> 
      <Setter Property="IsChecked" Value="{Binding Path=DecimalPlaces}" /> 
     </Style> 
    </MenuItem.ItemContainerStyle> 
</MenuItem>