簡體   English   中英

WPF Combobox XML綁定

[英]WPF Combobox XML binding

我一直無法找到適合我嘗試做的XML綁定的特定情況的東西。

我有以下xml片段:

我想將type屬性綁定到用戶控件上的組合框,但是讓可選值的列表由用戶控件上的列表驅動。

在用戶控件上,我創建可能值的列表:

<UserControl.Resources>
    <x:Array x:Key="HealingComponentTypes" Type="sys:String">
        <sys:String>Absolute</sys:String>
        <sys:String>AbsolutePercent</sys:String>
        <sys:String>RelativePercent</sys:String>
    </x:Array>
</UserControl.Resources>

然后我將它們綁定到組合框

<GroupBox DataContext="{Binding XPath=FieldHealingComponent}"  Header="Field Use" Height="328" HorizontalAlignment="Left" Margin="231,2,0,0" Name="groupBox3" VerticalAlignment="Top" Width="220">
            <Grid>
                <GroupBox Header="HP Healing Component" Height="101" HorizontalAlignment="Left" Margin="6,0,0,0" Name="HPFieldUseGroup" VerticalAlignment="Top" Width="192">
                    <Grid>
                        <ComboBox ItemsSource="{StaticResource HealingComponentTypes}" SelectedItem="{Binding XPath=HP/@type}" Height="23" HorizontalAlignment="Left" Margin="62,49,0,0" Name="MPBattleUseType" VerticalAlignment="Top" Width="112" />
                    </Grid>
                </GroupBox>
           </Grid>
        </GroupBox>

問題在於這不會綁定值。 我在下拉列表中看到了三種可能性,但是無論我選擇什么,它都不會反映到xml中。 同樣,即使已設置,初始值也不會顯示在組合框中。 我知道基本數據上下文是正確的,因為我在組框中有一個標簽

<TextBox Text="{Binding XPath=HP/@amount}"  Height="23" HorizontalAlignment="Right" Margin="0,16,6,0" Name="HPFieldHealingAmount" VerticalAlignment="Top" Width="112" />

並正確顯示金額。 我確定這是我不知道如何正確設置綁定的原因。 我嘗試了很多建議,這些建議在我用Google搜索時顯示出來,但沒有一個起作用。 任何幫助,將不勝感激。 謝謝

您是否在綁定中嘗試過Mode = TwoWay?

您是否嘗試過使用轉換器? 我相信bound屬性的類型不是字符串類型,您將需要IValueConverter來正確讀取和更新XML。

暫無
暫無

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

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