【发布时间】:2020-05-19 11:58:49
【问题描述】:
如果您在数据上下文更改的控件内创建单选按钮组。当您将数据上下文从稍后定义的单选按钮为真的条目更改为它为假但之前定义的单选按钮为真的条目时,原始项目的绑定值将更新为假。
您如何解决此问题? (虽然代码在 VB 中,但它适用于任何 .net 风格。我使用 dotnet 4.5.2 进行复制)
你可以在这里https://github.com/PhoenixStoneham/RadioButtonGroupBindinghttps://github.com/PhoenixStoneham/RadioButtonGroupBindinggithub上找到一个最小的问题解决方案
主窗口
<Window x:Class="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:WPFRadioButtonGroupBinding"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Durations}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedDuration}"/>
<Grid Grid.Column="1" DataContext="{Binding SelectedDuration}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Name"/>
<TextBlock Text="{Binding Name}" Grid.Column="1"/>
<Label Content="Duration" Grid.Row="1"/>
<TextBox Text="{Binding Frequency}" Grid.Row="1" Grid.Column="1"/>
<RadioButton GroupName="DurationType" IsChecked="{Binding Hourly}" Grid.Row="2" Grid.Column="1" Content="Hours"/>
<RadioButton GroupName="DurationType" IsChecked="{Binding Daily}" Grid.Row="3" Grid.Column="1" Content="Days"/>
<RadioButton GroupName="DurationType" IsChecked="{Binding Weekly}" Grid.Row="4" Grid.Column="1" Content="Weeks"/>
<RadioButton GroupName="DurationType" IsChecked="{Binding Monthly}" Grid.Row="5" Grid.Column="1" Content="Months"/>
</Grid>
</Grid>
</Window>
主窗口视图模型
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class MainWindowViewModel
Implements INotifyPropertyChanged
Public ReadOnly Property Durations As ObservableCollection(Of DurationViewModel)
Public Sub New()
Durations = New ObservableCollection(Of DurationViewModel)
Durations.Add(New DurationViewModel("Daily", 1, False, True, False, False))
Durations.Add(New DurationViewModel("Weekly", 1, False, False, True, False))
Durations.Add(New DurationViewModel("Fortnightly", 1, False, False, True, False))
Durations.Add(New DurationViewModel("Monthly", 1, False, False, False, True))
Durations.Add(New DurationViewModel("1/2 yearly", 6, False, False, False, True))
Durations.Add(New DurationViewModel("Other Days", 2, False, True, False, False))
Durations.Add(New DurationViewModel("Take Over", 1, True, False, False, False))
Durations.Add(New DurationViewModel("1/2 Day Takeover", 12, True, False, False, False))
End Sub
Private _SelectedDuration As DurationViewModel
Public Property SelectedDuration As DurationViewModel
Get
Return _SelectedDuration
End Get
Set(value As DurationViewModel)
_SelectedDuration = value
DoPropertyChanged("SelectedDuration")
End Set
End Property
Public Sub DoPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
持续时间视图模型
Imports System.ComponentModel
Public Class DurationViewModel
Implements INotifyPropertyChanged
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
DoPropertyChanged("Name")
End Set
End Property
Private _Hourly As Boolean
Public Property Hourly As Boolean
Get
Return _Hourly
End Get
Set(value As Boolean)
_Hourly = value
DoPropertyChanged("Hourly")
End Set
End Property
Private _Daily As Boolean
Public Property Daily As Boolean
Get
Return _Daily
End Get
Set(value As Boolean)
_Daily = value
DoPropertyChanged("Daily")
End Set
End Property
Private _Weekly As Boolean
Public Property Weekly As Boolean
Get
Return _Weekly
End Get
Set(value As Boolean)
_Weekly = value
DoPropertyChanged("Weekly")
End Set
End Property
Private _Monthly As Boolean
Public Property Monthly As Boolean
Get
Return _Monthly
End Get
Set(value As Boolean)
_Monthly = value
DoPropertyChanged("Monthly")
End Set
End Property
Public Sub New(name As String, frequency As Integer, hourly As Boolean, daily As Boolean, weekly As Boolean, monthly As Boolean)
Me.Name = name
Me.Frequency = frequency
Me.Hourly = hourly
Me.Daily = daily
Me.Weekly = weekly
Me.Monthly = monthly
End Sub
Private _Frequency As Integer
Public Property Frequency As Integer
Get
Return _Frequency
End Get
Set(value As Integer)
_Frequency = value
DoPropertyChanged("Frequency")
End Set
End Property
Public Sub DoPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
【问题讨论】:
-
我怀疑你需要为它写一个替换控件;我知道这是我需要做的,以解决内置单选按钮控件中的绑定更新行为问题。我制作了一个包含单选按钮的新用户控件。不幸的是,我没有在我的代码中评论我遇到的确切问题,但它看起来主要是围绕加载控件时的行为(值被推送到应该被拉入的绑定点,或者反之亦然)。
-
您是否使用了 GroupName 功能?
-
是的,我使用的是 GroupName。我在用户控件上实现了 GroupName、Content、ToolTip 和 IsChecked。
-
你会碰巧有那个用户控件的副本吗?如果我偷了它你介意吗?
-
我不确定,但这个问题可能会有所帮助:github.com/dotnet/wpf/issues/2995
标签: .net wpf vb.net data-binding