已经编写了这段代码,实际上是我对Microsoft教程进行的简单修改:https://docs.microsoft.com/de-de/dotnet/framework/wpf/data/how-to-use-the-master-detail-pattern-with-hierarchical-xml-data
我不明白为什么单选按钮选择在更改后不保持其状态,可以说除法,例如这种模式:
收件人:
联盟A-> B部门-> A部门
您会看到 Team III 的单选按钮不再处于选中状态。
该如何解决?
这是此代码段:
MainWindow.xml
<Window x:Class="MasterDetailXml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MasterDetailXml"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="MyList" Source="Data\Leagues.xml"
XPath="Leagues/League"/>
<DataTemplate x:Key="DataTemplateX">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
<Style TargetType="StackPanel">
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="Margin" Value="10,0,10,0"/>
</Style>
<Style TargetType="ListBox">
<Setter Property="Height" Value="100"/>
</Style>
<Style TargetType="Label">
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>
<DockPanel DataContext="{Binding Source={StaticResource MyList}}">
<StackPanel>
<Label>My Soccer Leagues</Label>
<ListBox ItemsSource="{Binding}"
ItemTemplate="{StaticResource DataTemplateX}"
IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel>
<Label Content="{Binding XPath=@name}"/>
<ListBox Name="divisionsListBox"
ItemsSource="{Binding XPath=Division}"
ItemTemplate="{StaticResource DataTemplateX}"
IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel>
<Label Content="{Binding XPath=@name}"/>
<ListView DataContext="{Binding ElementName=divisionsListBox,
Path=SelectedItem}"
ItemsSource="{Binding XPath=Team}"
IsSynchronizedWithCurrentItem="True">
<ListView.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding XPath=@name}" GroupName="Teams">
</RadioButton>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DockPanel>
</Window>
数据:
<?xml version="1.0" encoding="utf-8" ?>
<Leagues xmlns="">
<League name="League A">
<Division name="Division A">
<Team name="Team I"/>
<Team name="Team II"/>
<Team name="Team III"/>
<Team name="Team IV"/>
<Team name="Team V"/>
</Division>
<Division name="Division B">
<Team name="Team Blue"/>
<Team name="Team Red"/>
<Team name="Team Yellow"/>
<Team name="Team Green"/>
<Team name="Team Orange"/>
</Division>
<Division name="Division C">
<Team name="Team East"/>
<Team name="Team West"/>
<Team name="Team North"/>
<Team name="Team South"/>
</Division>
</League>
<League name="League B">
<Division name="Division A">
<Team name="Team 1"/>
<Team name="Team 2"/>
<Team name="Team 3"/>
<Team name="Team 4"/>
<Team name="Team 5"/>
</Division>
<Division name="Division B">
<Team name="Team Diamond"/>
<Team name="Team Heart"/>
<Team name="Team Club"/>
<Team name="Team Spade"/>
</Division>
<Division name="Division C">
<Team name="Team Alpha"/>
<Team name="Team Beta"/>
<Team name="Team Gamma"/>
<Team name="Team Delta"/>
<Team name="Team Epsilon"/>
</Division>
</League>
</Leagues>