2014-02-13 94 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>