wpf ListView中的单选按钮,SelectedItem不起作用[重复]

w41d8nur  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(287)

此问题在此处已有答案

WPF ListView with group of RadioButtons and select default value(1个答案)
25天前关闭。
在我的视图模型中有两个属性,INotifyPropertyChanged已实现但未在此处显示。两个属性都在构造函数中初始化并设置值。

public ObservableCollection<Foo> Foos { get; init;}
public Foo SelectedFoo { get => _selectedFoo; set => _selectedFoo = value;}

foo类:

public class Foo
{
   public string NameOfFoo { get; set; }
   public bool IsActive { get; set; }
}

在相应的视图中...

<ListView 
        ItemsSource="{Binding Foos}"
        SelectedItem="{Binding SelectedFoo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        Background="#FF063852" 
        BorderThickness="0">
        
        <ListView.ItemTemplate>
            <DataTemplate>
                
                <RadioButton Content="{Binding NameOfFoo}" 
                             GroupName="GroupSelector" 
                             BorderThickness="0"
                             Style="{StaticResource RadioButtonStyle}"
                             IsChecked="{Binding IsActive}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Checked">
                            <i:InvokeCommandAction Command="{Binding Path=DataContext.SelectGroupCommand, 
                                                                     RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                                   CommandParameter="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </RadioButton>
                

            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" Margin="5" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>

    </ListView>

ListView的ItemsSource工作正常。
从视图模型创建新示例后,未按预期选中radioButton。事件“checked”的命令正常。从未调用SelectedFoo属性中的SETTER。为什么?
我该怎么做,才能在创建后在视图中选中属性单选按钮?

相关问题