andreapier
andreapier

Reputation: 2958

Advanced Binding (first to xml then to something else)

first of all sorry for the title. I know this is not so clear but i cannot think of anything else. If someone wants me to change it, give your suggestions please (and please note that downvoting is not the right solution here) thanks. Now my problem. I have an xml that look like this:

<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>

I have a usercontrol in which the user can see and edit this scenarios:

<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>

So basically i want to:

System.Windows.Data Error: 43 : BindingExpression with XPath cannot bind to non-XML object.; XPath='lights/lights-group' BindingExpression:Path=; DataItem='XmlDataCollection' (HashCode=56809051); target element is 'ListBox' (Name='LightsList'); target property is 'ItemsSource' (type 'IEnumerable') XmlDataCollection:'MS.Internal.Data.XmlDataCollection'

Can you help me please?

Upvotes: 0

Views: 341

Answers (1)

andreapier
andreapier

Reputation: 2958

I managed to get i tworking with this code:

<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>

So the key seems to be IsSynchronized="true". Hope this is useful for other people.

Upvotes: 1

Related Questions