Combobox usercontrol在WPF容器中的SelectedItem数据绑定

时间:2017-05-10 12:36:19

标签: wpf combobox

这是我的组合框用户控件:

<UserControl x:Class="Hexa.Screens.UsrColorPicker"
    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:Hexa.Screens"
    xmlns:sys="clr-namespace:System;assembly=mscorlib" Height="40" Width="200" Name="uccolorpicker"
    mc:Ignorable="d">
<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider MethodName="GetType"  ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
            <ObjectDataProvider.MethodParameters>
                <sys:String>System.Windows.Media.Colors, PresentationCore,  Version=3.0.0.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"  MethodName="GetProperties" x:Key="colorPropertiesOdp"/>
    </ResourceDictionary>
</UserControl.Resources>

<ComboBox Name="superCombo" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name"  SelectedValue="{Binding ElementName=uccolorpicker, Path=SelectedColor}" Text="{Binding ElementName=uccolorpicker,Path=Text}" SelectedItem="{Binding ElementName=uccolorpicker, Path=SelectedItem}"  SelectedIndex="{Binding ElementName=uccolorpicker, Path=SelectedIndex}" SelectionChanged="superCombo_SelectionChanged"   HorizontalContentAlignment="Stretch" >
    <ComboBox.ItemTemplate>
        <DataTemplate >
            <WrapPanel Orientation="Horizontal" Background="Transparent">
                <TextBlock Width="20" Height="20" Margin="5" Background="{Binding Name}"  />
                <TextBlock  Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" FontStyle="Italic" FontWeight="Bold" FontFamily="Palatino Linotype" />
            </WrapPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

这是背后的代码:

  namespace Hexa.Screens
  {
      /// <summary>
      /// Interaction logic for UsrColorPicker.xaml
      /// </summary>
      public partial class UsrColorPicker : UserControl
      {
          public UsrColorPicker()
          {
              InitializeComponent();
          }

          public Brush SelectedColor
          {
              get { return (Brush)GetValue(SelectedColorProperty); }
              set { SetValue(SelectedColorProperty, value); }
          }

          public int SelectedIndex
          {
              get { return (int)GetValue(SelectedIndexProperty); }
              set { SetValue(SelectedIndexProperty, value); }
          }

          public int SelectedItem
          {
              get { return (int)GetValue(SelectedItemProperty); }
              set { SetValue(SelectedItemProperty, value); }
          }

          public string Text
          {
              get { return (string)GetValue(SelectedTextProperty); }
              set { SetValue(SelectedTextProperty, value); }
          }


          // Using a DependencyProperty as the backing store for SelectedColor.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty SelectedColorProperty =
              DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(UsrColorPicker), new UIPropertyMetadata(null));

          public static readonly DependencyProperty SelectedIndexProperty =
              DependencyProperty.Register("SelectedIndex", typeof(int), typeof(UsrColorPicker), new UIPropertyMetadata(null));

          public static readonly DependencyProperty SelectedItemProperty =
             DependencyProperty.Register("SelectedItem", typeof(int), typeof(UsrColorPicker), new UIPropertyMetadata(null));

          public static readonly DependencyProperty SelectedTextProperty =
            DependencyProperty.Register("Text", typeof(int), typeof(UsrColorPicker), new UIPropertyMetadata(null));




          public static readonly RoutedEvent SettingConfirmedEvent =
          EventManager.RegisterRoutedEvent("SettingConfirmedEvent", RoutingStrategy.Bubble,
          typeof(RoutedEventHandler), typeof(UsrColorPicker));

          public event RoutedEventHandler SettingConfirmed
          {
              add { AddHandler(SettingConfirmedEvent, value); }
              remove { RemoveHandler(SettingConfirmedEvent, value); }
          }

          private void superCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              RaiseEvent(new RoutedEventArgs(UsrColorPicker.SettingConfirmedEvent));
          }
      }
 }

我正在尝试在我的容器的XAML中设置其SelectedItem,SelectedIndex到xaml绑定,如下所示: -

<local:UsrColorPicker x:Name="cmbItem_Group_back_color"  HorizontalAlignment="Center" Width="205" Height="22"   SettingConfirmed="cmbItem_Group_back_color_SettingConfirmed"  SelectedColor ="{Binding Path=CurrentRec.Primary_Tone,Mode=TwoWay}"     Canvas.Left="97" Canvas.Top="92"    />

背后的代码如下: -

 form_load()
 {
 this.DataContext = DataContract_ButtonSettings;
 }

但是选定项目的文字并没有在组合框中显示出来。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这是一个不小心的错误.. 这个bug在发布的代码中没有。实际上,我使用usercontrol的selectedcolor属性绑定到视图中元素的backcolor属性.viewmodel更新ColorCombobox的SelectedColor属性.Viewmodel正在从许多地方更新在一个地方,viewmodel的SelectedColor属性被设置为HEX等效于已知Color of System.Windows.Media.Colors已知颜色,当视图模型wud尝试绑定到该Hex元素时在ComboBox中,它没有在下拉列表中找到该Hex值的任何匹配条目,因此,虽然控件的背景颜色正在生效,但组合框文本显示为空白。:-)。 无论如何,解决了...感谢你的时间Ed。 : - 。)

相关问题