XAML XPath绑定,DataGrid + ComboBox With Xml

lrpiutwd  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(123)

我真的需要你的帮助。这个组合框显示了XML数据,就像我希望的那样工作。数据网格的工作是从XML数据中提取单个记录。我想做的是我正在努力的事情。

  • Make是这样的,当选择组合框项时,它会更改数据网格的Xpath以显示该计划。

我玩过很多东西,比如多重绑定,但我似乎就是不能让它正常工作。任何帮助将不胜感激。

<Window.Resources>

      <!-- Data. -->
           <XmlDataProvider x:Key="Schedules" XPath="Schedules">
             <x:XData>
               <Schedules xmlns="">
                 <Schedule Id="0" Name="Week-1"  Saturday="0" Sunday="0" Monday="7.5" Tuesday="7.5" Wednesday="7.5" Thursday="7.5" Friday="7.5" />
                 <Schedule Id="1" Name="Week-2"  Saturday="0" Sunday="0" Monday="7.5" Tuesday="7.5" Wednesday="7.5" Thursday="7.5" Friday="7.5" />
                 <Schedule Id="2" Name="Week-3"  Saturday="7.5" Sunday="7.5" Monday="7.5" Tuesday="7.5" Wednesday="7.5" Thursday="0" Friday="0" />
               </Schedules>
             </x:XData>
           </XmlDataProvider>

    </Window.Resources>
        
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
          <ComboBox Name="SchedulesComboBox" Width="200" Height="40" VerticalContentAlignment="Center" 
             ItemsSource="{Binding XPath=//Schedule/@Name, Source={StaticResource Schedules}}" />
        </StackPanel>

        <StackPanel Grid.Row="0">
          <DataGrid Name="DataGrid" Margin="0, 25, 0, 0" Grid.Row="1" ItemsSource="{Binding XPath=//*[@Id\=\'1\'], 
              Source={StaticResource Schedules}, IsAsync=True}" AutoGenerateColumns="False">
            <DataGrid.Columns>
              <DataGridComboBoxColumn Header="Schedule" Width="200" />
              <DataGridTextColumn Header="Saturday" Width="100" Binding="{Binding XPath=@Saturday}" />
              <DataGridTextColumn Header="Sunday" Width="100" Binding="{Binding XPath=@Sunday}" />
              <DataGridTextColumn Header="Monday" Width="100" Binding="{Binding XPath=@Monday}" />
              <DataGridTextColumn Header="Tuesday" Width="100" Binding="{Binding XPath=@Tuesday}" />
              <DataGridTextColumn Header="Wednesday" Width="100" Binding="{Binding XPath=@Wednesday}" />
              <DataGridTextColumn Header="Thursday" Width="100" Binding="{Binding XPath=@Thursday}" />
              <DataGridTextColumn Header="Friday" Width="100" Binding="{Binding XPath=@Friday}" />
            </DataGrid.Columns>
          </DataGrid>
        </StackPanel>

    </Grid>
iecba09b

iecba09b1#

我从来没有使用过XPath,我能给予的唯一提示是使用ViewModel,将ComboBox的SelectedItem绑定到视图模型,并将DataGrid.ItemsSource绑定到视图模型中的某个派生属性。
或者这不是一个选择?

相关问题