为什么我的用户控件与组合框绑定不正确?



我有一个非常简单的用户控件,我正在尝试创建它,其中包含美国各州的列表。我正在尝试通过"选定状态"属性公开所选状态。但是,一旦它挂接到另一个 UserControl/窗体中,我就无法尝试让此绑定触发。

用户控件的 XAML 如下所示:

<UserControl x:Class="Sample.Desktop.UserControls.StateDropdown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:Sample.Desktop.UserControls"
mc:Ignorable="d" 
Width="170" Height="28"
d:DesignHeight="28" d:DesignWidth="170">
<ComboBox x:Name="cboState" 
ItemsSource="{Binding StateList, RelativeSource={RelativeSource AncestorType=UserControl}}" 
SelectedValue="{Binding SelectedState, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" 
>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Abbreviation}"></Label>
<Label> - </Label>
<Label Content="{Binding Name}"></Label>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

在代码隐藏中,我有以下代码:

public static readonly DependencyProperty SelectedStateProperty = DependencyProperty.Register("SelectedState", 
                            typeof(USState), 
                            typeof(StateDropdown), 
                            new UIPropertyMetadata(null, 
                                                   new PropertyChangedCallback(OnSelectedStateChanged), 
                                                   new CoerceValueCallback(OnCoerceSelectedState)));
private static object OnCoerceSelectedState(DependencyObject o, object value)
{
StateDropdown stateDropdown = o as StateDropdown;
if (stateDropdown != null)
return stateDropdown.OnCoerceSelectedState((USState)value);
else
return value;
}
private static void OnSelectedStateChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
StateDropdown stateDropdown = o as StateDropdown;
if (stateDropdown != null)
stateDropdown.OnSelectedStateChanged((USState)e.OldValue, (USState)e.NewValue);
}
protected virtual USState OnCoerceSelectedState(USState value)
{
// TODO: Keep the proposed value within the desired range.
return value;
}
protected virtual void OnSelectedStateChanged(USState oldValue, USState newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
public USState SelectedState
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (USState)GetValue(SelectedStateProperty);
}
set
{
SetValue(SelectedStateProperty, value);
}
}

我无法触发 SelectedState 的 SelectedValue 绑定属性,所以我最终挂接了 SelectionChanged 事件。

private void cboState_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems?.Count > 0)
{
SelectedState = (USState)e.AddedItems[0];
}
}

在我的其他用户控件中,我在 XAML 中有以下内容:

<uc:StateDropdown Margin="10,0,0,0" SelectedState="{Binding SelectedState}" ></uc:StateDropdown>

和视图模型(我正在使用Caliburn Micro),我有这个属性:

protected USState _selectedState;
public USState SelectedState
{
get { return _selectedState; }
set
{
_selectedState = value;
NotifyOfPropertyChange(() => SelectedState);                
}
}

组合按预期填充。但是,当我更改选择时,永远不会触发/更新 SelectedState。

我以前也尝试过使用SelectedItem而不是SelectedValue,结果相同。

我确定我错过了一些明显的东西,但我很难看出我哪里出错了。

编辑:这是修复绑定的方法。

我删除了 SelectionChanged 事件。然后我修改了我的"托管页面"用户控件以设置 TwoWay 绑定:

<uc:StateDropdown Margin="10,0,0,0" SelectedState="{Binding SelectedState, Mode=TwoWay}" ></uc:StateDropdown>

添加后,当我更改组合框值时,SelectedState 就开始更新。

我唯一看到的是这一行:

SelectedValue="{Binding SelectedState, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" 

您不需要它,因为 SelectionChanged 事件。它可能会导致问题。

此外,我将使用双向绑定绑定用户控件的选定状态。

希望对您有所帮助。