代码之家  ›  专栏  ›  技术社区  ›  Sturm

单选按钮弄乱绑定数据

  •  0
  • Sturm  · 技术社区  · 7 年前

    UserControl ( TaskView1 )我把几个布尔值绑定到 RadioButtons :

          <StackPanel>
                    <RadioButton Content="Spell" IsChecked="{Binding IsSpell}"  />
                    <RadioButton Content="Movement" IsChecked="{Binding IsMovement}"/>
                    <RadioButton Content="Action" IsChecked="{Binding IsAction}" />
                    <RadioButton Content="Find" IsChecked="{Binding IsFind}"/>
           </StackPanel>
    

    用户控件 ( 任务视图1 )用于显示选定的 Task 在一个 TreeView (注意,我有两个 DataTemplate 因为有两种不同类型的 任务 我希望每个人都有不同的观点)。

     <GroupBox Header="Selected Task">
                <GroupBox.Resources>
                    <DataTemplate DataType="{x:Type local:Task1}">
                        <local:TaskView1/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:Task2}">
                        <local:TaskView2/>
                    </DataTemplate>
                </GroupBox.Resources>
                <Grid HorizontalAlignment="Left">
                    <ScrollViewer DataContext="{Binding SelectedItem, ElementName=TaskTreeView}" Content="{Binding}"/>
                </Grid>
     </GroupBox>
    

    当我更改树中任务的选择时,会出现问题。由于单选按钮是排他性的,我经常发现 Task1Instance1 在树中选择时已更改 Task1Instance2 .

    例子:

    在树中选择任务A。这是一个 Movement . 然后用户选择任务B,它是 Spell . 发生的情况是,任务A的数据以某种方式发生了更改 IsMovement 为false,因此如果用户再次选择任务A,则不会选择单选按钮。

    问题:

    这是WPF中的常见问题还是我只是搞砸了什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sturm    7 年前

    问题是一个组合框在XAML中将SelectedIndex属性设置为=“0”。