簡體   English   中英

XAML中的動態綁定

[英]Dynamic Bindings in 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>

這至少會使菜單顯示出來,但是問題是DecimalPlaces處理整數(我現在只是將oneDecimal和twoDecimal用作占位符),並且我希望動態資源是一個整數,最好是從1變為10太。

所以我的問題是:如何將動態資源設置為整數而不是特定變量,並且有可能動態生成此菜單的方法(與編寫10個不同的條目相對),也許是基於數組或其他方法?

很抱歉,如果這是一個非常簡單的問題,如我所說,我的XAML有點弱。 任何幫助,不勝感激。

如果我正確理解了您的問題,則認為您不需要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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM