高级绑定(首先是xml然后是其他东西)

时间:2012-01-20 10:21:18

标签: c# xaml data-binding

首先抱歉标题。我知道这不是那么清楚,但我想不出别的什么。如果有人要我改变它,请提出你的建议(请注意,downvoting不是正确的解决方案)谢谢。 现在我的问题。 我有一个看起来像这样的xml:

<scenarios-list>


<scenario name="Fuori casa" value="fuori-casa">
    <lights>
        <lights-group name="soggiorno">
          <lights-item name="portone" outputChannelName="Luci|Appartamento|Portone|DO_Stato" inputChannelName="Luci|Appartamento|Portone|DI_On_Off">off</lights-item>
          <lights-item name="soggiorno-principale" outputChannelName="Luci|Appartamento|Soggiorno_Principale|DO_Stato" inputChannelName="Luci|Appartamento|Soggiorno_Principale|DI_On_Off">off</lights-item>
          <lights-item name="soggiorno-secondario" outputChannelName="Luci|Appartamento|Soggiorno_Secondario|DO_Stato" inputChannelName="Luci|Appartamento|Soggiorno_Secondario|DI_On_Off">off</lights-item>
          <lights-item name="parete-zona-cucina" outputChannelName="Luci|Appartamento|Parete_Zona_Cucina|DO_Stato" inputChannelName="Luci|Appartamento|Parete_Zona_Cucina|DI_On_Off">off</lights-item>
          <lights-item name="finestra-soggiorno" outputChannelName="Luci|Appartamento|Finestra_Soggiorno|DO_Stato" inputChannelName="Luci|Appartamento|Finestra_Soggiorno|DI_On_Off">off</lights-item>
        </lights-group>

        ... (lots of <lights-group> here)

    </lights>

    ... (lots of <lights> here)

</scenario>

...(lots of <scenario> here)

</scenarios-list>

我有一个usercontrol,用户可以在其中查看和编辑这些场景:

<Grid Name="BaseGrid">

        <Grid.Resources>
            <XmlDataProvider **x:Name="ScenesXmlName"** x:Key="ScenesXml" XPath="scenari-list/scenario" Source="C:\Users\andrea\RSSReaderSubscriptions-5.xml"/>
        </Grid.Resources>

        <ComboBox Name="ScenariCombo"
                  **ItemsSource="{Binding Source={StaticResource ScenesXml}}" DisplayMemberPath="@name"**/>


        <StackPanel>
                <ListBox Name="LightsList" **ItemsSource="{Binding Source={StaticResource ScenesXml}, XPath=lights/lights-group}**">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Expander Name="LightsExpander" **Header="{Binding XPath=@name}"** >
                                <ListBox>
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <Label **Content="{Binding XPath=ligths-item@name}"** />
                                                <CheckBox />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </Expander>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </StackPanel>
    </Grid>

基本上我想:

  • <scenarios-list>加载到组合框中(仅显示 方案的名称)[我的代码已经这样做]
  • 根据所选的组合框项目将<lights-group>列表加载到列表框中(仅显示灯组名称)[即使出现此错误,我的代码也已执行此操作:
  

System.Windows.Data错误:43:BindingExpression与XPath无法绑定到非XML对象。的XPath =&#39;灯/熄灯组&#39; BindingExpression:路径=;的DataItem =&#39; XmlDataCollection&#39; (的HashCode = 56809051);目标元素是&#39; ListBox&#39; (名称=&#39; LightsList&#39);目标属性是&#39; ItemsSource&#39; (键入&#39; IEnumerable&#39;)XmlDataCollection:&#39; MS.Internal.Data.XmlDataCollection&#39;

  • 在每个Expander中加载<lights-item>中包含的所有<lights-group> [我不知道如何做到这一点]

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

我设法使用此代码:

<Grid Name="BaseGrid">

    <Grid.Resources>
        <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml" XPath="scenari-list/scenario" Source="myPath"/>
    </Grid.Resources>

    <ComboBox Grid.Column="0" Grid.Row="1" Name="ScenariCombo" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource ScenesXml}}" DisplayMemberPath="@name"/>


<StackPanel>
<ListBox Name="LightsList" ItemsSource="{Binding Source={StaticResource ScenesXml}, XPath=lights/lights-group}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Expander Header="{Binding XPath=@name}" HorizontalAlignment="Stretch">
                                    <ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding XPath=lights-item}">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="150" />
                                                    </Grid.ColumnDefinitions>
                                                    <Label Content="{Binding XPath=@name}" Grid.Column="0"/>
                                                    <CheckBox Grid.Column="1" IsChecked="{Binding XPath=., Converter={StaticResource myStateToBoolConverter}}"/>
                                                </Grid>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>
                                </Expander>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
    </StackPanel>


</Grid>

所以键似乎是Synchronized =“true”。 希望这对其他人有用。