WPF Mvvm 单选按钮绑定未从视图模型设置初始值

发布于 2024-11-05 09:23:33 字数 1257 浏览 0 评论 0原文

我在使用单选按钮组时遇到了很多麻烦。最初,单选按钮不会设置 ViewModel 中的值,但选择后可以正常工作。所以基本上用户不知道初始值是多少。

这是我的 xaml。

<ListBox ItemsSource="{Binding ViewModelCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="rbList" IsChecked="{Binding Path=IsReady,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                            <Label Content="{Binding Path=NameOfRadioButton}"></Label>
                        </RadioButton>
                    </DataTemplate>
                </ListBox.ItemTemplate>    
            </ListBox>

这是我的 ViewModel,

public bool IsReady
        {
            get
            {
                return BusinessObject.IsReady;  // debuggin on this line shows a true value being returned
            }
            set
            {
                BusinessObject.IsReady = value;
                OnPropertyChanged("IsReady");
            }
        }

Viewmodel 是一个简单的 bool 值,它实现了 INotifyPropertyChanged。 我怀疑我是否可以使用枚举转换器,因为单选按钮的数量始终是动态的。

所以为了澄清一下,第一次运行时没有选择单选按钮,但单击后单选按钮工作正常。视图模型和底层数据库反映了所做的更改。

如何让单选按钮在启动时显示值?

干杯。

I am having quite a bit of trouble with a radio button group. initially the radio buttons do not set the value from the ViewModel but when selected function properly. so basically the user does not know what is the initial value.

here is my xaml.

<ListBox ItemsSource="{Binding ViewModelCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="rbList" IsChecked="{Binding Path=IsReady,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                            <Label Content="{Binding Path=NameOfRadioButton}"></Label>
                        </RadioButton>
                    </DataTemplate>
                </ListBox.ItemTemplate>    
            </ListBox>

here is my ViewModel

public bool IsReady
        {
            get
            {
                return BusinessObject.IsReady;  // debuggin on this line shows a true value being returned
            }
            set
            {
                BusinessObject.IsReady = value;
                OnPropertyChanged("IsReady");
            }
        }

the Viewmodel is a simple bool value what implements INotifyPropertyChanged.
i doubt i can use an enum converter as the amount of radio buttons is always dynamic.

So just to clarify, on first run no radio button is selected but after click the radiobuttons work fine. and the viewmodel and underlying database reflect the changes made.

How can i get the radio buttons to show value on startup?

cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-11-12 09:23:33

使用 WPF 工具包主题 (http://wpf.codeplex.com/) 时遇到同样的问题。我比较了 CheckBox(有效)和 RadioButton(无效)的控件模板。 IsChecked 值的触发器设置为与复选框相反,它查找“false”,然后在退出时执行选中的动画,其中复选框查找 true。

当单选按钮在渲染之前被绑定时,这会导致问题,因为该值已经是 true,设置选中的视觉状态的动画不会触发,直到您选中另一个框以使其变为 false。

我修复了单选按钮主题,使其遵循与复选框相同的视觉状态逻辑。我不确定这是否会破坏单选按钮的任何其他功能,但到目前为止它似乎工作正常。

Had the same problem with using the WPF toolkit themes (http://wpf.codeplex.com/). I compared the control template of the CheckBox (which worked) and the RadioButton (which did not work). The trigger for the IsChecked value is set to work opposite of the checkbox, it looks for "false" and then performs the checked animation on exit, where checkbox looks for true.

This causes a problem when a radio button is bound before it is rendered since the value would already be true the animation to set the checked visual state wouldn't fire until you checked another box to make it false.

I fixed the radio button theme by making it follow the same visual state logic as the checkbox. I am not sure if this breaks any other features of the radio button, but it seems to be working fine so far.

歌入人心 2024-11-12 09:23:33

我同意用户2174874提供的答案。我想做的就是添加 xaml 代码来解决问题。

有问题的代码:

    <Trigger Property="IsChecked" Value="false">

        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.EnterActions>

    </Trigger>
    <Trigger Property="IsChecked" Value="True" />

更正的代码:

    <Trigger Property="IsChecked" Value="True">
        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.EnterActions>
    </Trigger>

I agree with the answer provided by user2174874. All I wish to do is add the xaml code that set things straight.

Problematic Code:

    <Trigger Property="IsChecked" Value="false">

        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.EnterActions>

    </Trigger>
    <Trigger Property="IsChecked" Value="True" />

Corrected Code:

    <Trigger Property="IsChecked" Value="True">
        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.EnterActions>
    </Trigger>
夏末染殇 2024-11-12 09:23:33

我已经将 ViewModelCollection 创建为 ObservableCollection,并在 ViewModel 的 ctor 中使用测试对象,它对我有用,所以我认为您应该从那里的数据库中获取数据并 RaisePropertyChanged 的​​ ViewModelCollection。

I have created ViewModelCollection as ObservableCollection with test objects in ViewModel's ctor and it worked for me so i think you should fetch data from database there and RaisePropertyChanged of ViewModelCollection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文