【问题标题】:XML Combo boxes cross binding with each otherXML 组合框相互交叉绑定
【发布时间】:2019-05-23 18:13:48
【问题描述】:

亲爱的,

我正在通过自定义 Autodesk DataStandard 插件的 UI 进入 XML 世界。

Combobox1 应填充 XML 文件中列出的 StandardTextInfo 项。每个项目都由以下属性组成:
-标准文本编号
-标准文本DE
-标准文本EN
-StandardTextFR

这是 StandardText.XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<StandardTextData xmlns="">
  <StandardTextInfo StandardTextNumber="10" DE="KATZE" EN="CAT" FR="CHAT"></StandardTextInfo>
  <StandardTextInfo StandardTextNumber="20" DE="HUND" EN="DOG" FR="CHIEN"></StandardTextInfo>
  <StandardTextInfo StandardTextNumber="30" DE="PFERD" EN="HORSE" FR="CHEVAL"></StandardTextInfo>
</StandardTextData>

Combobox2 应填充 XML 文件中列出的 LanguageInfo 项。每个项目都由属性 LanguageName (DE=Deutsch, EN=English, FR=Francais) 组成。这是 Language.XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LanguageData xmlns="">
  <LanguageInfo LanguageName="DE"></LanguageInfo>
  <LanguageInfo LanguageName="EN"></LanguageInfo>
  <LanguageInfo LanguageName="FR"></LanguageInfo>
</LanguageData>

以下是预期用途:
-用户从Combobox2中选择项目将在Combobox1中显示的语言。
- Combobox1 DisplayMemberPath 中显示的值必须与 Combobox2 中选择的值对应的 items 属性绑定。因此,项目名称可以以用户选择的语言显示。
- 此外,Combobox3 显示 Combobox1 中所选项目的属性 StandardTextNumber。通过使用组合框(我谈论 Combobox3)而不是标签,用户还可以选择直接选择项目属性 StandardTextNumber,这会反向更新显示的项目Combobox1(仍使用 Combobox2 中定义的语言)。

这是 XAML 文件中 3 个组合框的定义:

    <?xml version="1.0" encoding="utf-8"?>
    <WPF:MainWindow>
        x:Name="FileWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPF="clr-namespace:CreateObject.WPF;assembly=CreateObject"

    <Window.Resources>
        <XmlDataProvider>
            x:Key="Languages"
            Source="C:\Language.xml"
            XPath="/LanguageData"
        </XmlDataProvider>
        <XmlDataProvider
            x:Key="StandardTexts"
            Source="C:\StandardText.xml"
            XPath="/StandardTextData"
        </XmlDataProvider>
    </Window.Resources>

    <ComboBox>
        x:Name="Combobox1"
        IsEnabled="{Binding CreateMode}"
        ItemsSource="{Binding Source={StaticResource StandardTexts}, XPath="StandardTextInfo"}"
        SelectedItem="{Binding SelectedItem, ElementName=ComboBox3, Mode=OneWay}"
        SelectedValue="{Binding SelectedValue, ElementName=ComboBox2, Mode=OneWay}"
        SelectedValuePath="@StandardTextNumber"
        DisplayMemberPath="{Binding SelectedValue, ElementName=ComboBox3, Mode=OneWay}"
    </ComboBox>

    <ComboBox>
        x:Name="Combobox2"
        IsEnabled="{Binding CreateMode}"
        ItemsSource="{Binding Source={StaticResource Languages}, XPath="LanguageInfo"}"
        SelectedValue="{Binding Prop[LanguageName].value}"
        SelectedValuePath="@LanguageName"
        DisplayMemberPath="@LanguageName"
    </ComboBox>

    <ComboBox>
        x:Name="Combobox3"
        IsEnabled="{Binding CreateMode}"
        ItemsSource="{Binding Source={StaticResource StandardTexts}, XPath="StandardTextInfo"}"
        SelectedItem="{Binding SelectedItem, ElementName=ComboBox1, Mode=OneWay}"
        SelectedValue="{Binding SelectedValue, ElementName=ComboBox2, Mode=OneWay}"
        SelectedValuePath="{Binding SelectedValue, ElementName=ComboBox1, Mode=OneWay}"
        DisplayMemberPath="@StandardTextNumber"
    </ComboBox>

    </WPF:MainWindow>

我的问题:
1) Combobox1 SelectedValue & DisplayMemberPath, Combobox3 SelectedValue & SelectedValuePath:我可能错过了逻辑:应该如何表达所需的绑定?

2) 在XML中,绑定属性前额@前缀的作用是什么?

3) 我很确定确实不需要源 Language.XML:是否可以直接从源中标识的语言填充 Combobox2 StandardText.XML?

4) IsEnabled 属性在什么情况下应该设置为 CreateMode 或 EditMode?​​p>

提前感谢您提供的善意建议。

最好的问候, 里昂

【问题讨论】:

  • 您好,我的要求是太简单还是太难了?

标签: xml combobox binding


【解决方案1】:

问题 1 的答案:

StandardText.XMLLanguage.XML 文件没有被修改(参见上面帖子中的描述)

3 个组合框 修改为 File.XAMLInventor.XAML 文件(抱歉,我无法突出显示修改):

<?xml version="1.0" encoding="utf-8"?>
<WPF:MainWindow>
    x:Name="FileWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPF="clr-namespace:CreateObject.WPF;assembly=CreateObject"

<Window.Resources>
    <XmlDataProvider>
        x:Key="Languages"
        Source="C:\Language.xml"
        XPath="/LanguageData"
    </XmlDataProvider>
    <XmlDataProvider
        x:Key="StandardTexts"
        Source="C:\StandardText.xml"
        XPath="/StandardTextData"
    </XmlDataProvider>
</Window.Resources>

<ComboBox>
    x:Name="Combobox1"
    IsEnabled="{Binding CreateMode}"
    ItemsSource="{Binding Source={StaticResource StandardTexts}, XPath="StandardTextInfo"}"
    SelectedItem="{Binding SelectedItem, ElementName=ComboBox3, Mode=OneWay}"
    # SelectedValue: no need to be defined
    # SelectedValuePath: no need to be defined
    DisplayMemberPath="{Binding ElementName=Combobox2, StringFormat=@{0}, Path=SelectedValue}"
</ComboBox>

<ComboBox>
    x:Name="Combobox2"
    IsEnabled="{Binding CreateMode}"
    ItemsSource="{Binding Source={StaticResource Languages}, XPath="LanguageInfo"}"
    SelectedValue="{Binding Prop[LanguageName].value}"
    SelectedValuePath="@LanguageName"
    DisplayMemberPath="@LanguageName"
</ComboBox>

<ComboBox>
    x:Name="Combobox3"
    IsEnabled="{Binding CreateMode}"
    ItemsSource="{Binding Source={StaticResource StandardTexts}, XPath="StandardTextInfo"}"
    SelectedItem="{Binding SelectedItem, ElementName=ComboBox1, Mode=OneWay}"
    # SelectedValue: no need to be defined
    SelectedValuePath="@StandardTextNumber"
    DisplayMemberPath="@StandardTextNumber"
</ComboBox>

</WPF:MainWindow>

终于不用那么多修改就可以得到预期的结果了。要点是:

-了解SelectedValueSelectedValuePath属性的用途

-绑定语法StringFormat=@{0}

问题 2 到 4 现在没有意义了。

非常感谢北京瑞恩莱软件科技有限公司的白雪松提供的解决方案!

最好的问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2022-06-22
    • 2011-07-07
    相关资源
    最近更新 更多