为什么我的用户无法正确使用组合框绑定进行控制?

时间:2017-04-14 12:15:37

标签: wpf xaml combobox user-controls

我有一个非常简单的UserControl我正在尝试创建包含美国州列表。我试图通过" SelectedState"暴露所选状态。属性。但是,一旦它在另一个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>

ViewModel(我使用Caliburn Micro),我有这个属性:

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

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

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

我确定我错过了一些明显的东西,但我在查看错误的位置时遇到了麻烦。

编辑:这是修复绑定的原因。

我删除了SelectionChanged事件。然后我修改了我的&#34;托管页面&#34; usercontrol设置TwoWay绑定:

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

我添加后,当我更改ComboBox值时,SelectedState开始更新。

1 个答案:

答案 0 :(得分:1)

我看到的唯一的事情就是这一行:

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

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

此外,我将使用TwoWay绑定绑定UserControl的SelectedState。

希望能帮助你。