Two-Way Binding Issue of Unknown Object in WPF Custom Control Dependency Property

心不动则不痛 提交于 2019-12-30 07:26:09

问题


I'm having a Custom Control - Implemented for AutoComplete TextBox. I got all idea's from the following question Create a Custom Control with the combination of multiple controls in WPF C#. In that Custom Control, they suggest the following code for adding item, its perfectly working and the Two-Way Binding too.

(this.ItemsSource as IList<string>).Add(this._textBox.Text);

But, I changed the following code to Unknown Object, so I changed IList<string> to IList<object>

(this.ItemsSource as IList<object>).Add(item);

XAML:

 <local:BTextBox 
            ItemsSource="{Binding Collection}" 
            ProviderCommand="{Binding AutoBTextCommand}" 
            AutoItemsSource="{Binding SuggCollection}" />

But it's not updating the ViewModel Property Collection. I too tried the following changes in the xaml

ItemsSource="{Binding Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

I don't know where I did the mistake.

Functionality: The TextBox inside the CustomControl takes the input from the User and it triggers the ProviderCommand, that Command filters the Remote data based on the User Input and sends the Filtered Collection via AutoItemsSource, this Property is binded as a ItemsSource of the ListBox inside that CustomControl to select the Item. We can select the Item from the ListBox Item, by clicking the Item, it triggers the Command AddCommand which is in the CustomControl Class, it add the selected item in ItemSource Property of the CustomControl. I'm having the Two-Way Binding issue in this Property ItemsSource. From this property only we may get the Selected item as a Collection.

Here is my Complete Source Code

The Custom Control C# Code:

public class BTextBox : ItemsControl
{

    static BTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BTextBox), new FrameworkPropertyMetadata(typeof(BTextBox)));
    }

    #region Private Members
    private TextBox _textBox;
    private ItemsControl _itemsView;
    #endregion

    #region Dependency Property Private Members
    public static readonly DependencyProperty ProviderCommandProperty = DependencyProperty.Register("ProviderCommand", typeof(ICommand), typeof(BTextBox), new PropertyMetadata(null));
    public static readonly DependencyProperty AutoItemsSourceProperty = DependencyProperty.Register("AutoItemsSource", typeof(IEnumerable<dynamic>), typeof(BTextBox), new PropertyMetadata(null, OnItemsSourceChanged));
    #endregion

    #region Dependency Property Public members
    public IEnumerable<dynamic> AutoItemsSource
    {
        get { return (IEnumerable<dynamic>)GetValue(AutoItemsSourceProperty); }
        set { SetValue(AutoItemsSourceProperty, value); }
    }
    #endregion

    #region Listener Methods
    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var tb = d as BTextBox;
        if ((e.NewValue != null) && ((tb.ItemsSource as IList<object>) != null))
        {
            (tb.AutoItemsSource as IList<object>).Add(e.NewValue);
        }
    }
    #endregion

    #region Override Methods
    public override void OnApplyTemplate()
    {
        this._textBox = this.GetTemplateChild("PART_TextBox") as TextBox;
        this._itemsView = this.GetTemplateChild("PART_ListBox") as ItemsControl;

        this._textBox.TextChanged += (sender, args) =>
        {
            if (this.ProviderCommand != null)
            {
                this.ProviderCommand.Execute(this._textBox.Text);
            }
        };

        base.OnApplyTemplate();
    }
    #endregion


    #region Command
    public ICommand ProviderCommand
    {
        get { return (ICommand)GetValue(ProviderCommandProperty); }
        set { SetValue(ProviderCommandProperty, value); }
    }

    public ICommand AddCommand
    {
        get
        {
            return new DelegatingCommand((obj) =>
            {
                (this.ItemsSource as IList<object>).Add(obj);
            });
        }
    }
    #endregion
}

The Generic.xaml Code is

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SampleControl">
    <Style TargetType="{x:Type local:BTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BTextBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBox x:Name="PART_TextBox" Grid.Row="0" Width="*" VerticalAlignment="Center" />
                            <ListBox ItemsSource="{TemplateBinding AutoItemsSource}" Grid.Row="1" x:Name="PART_ListBox_Sugg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="{Binding Value.IsChecked}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BTextBox}}, Path=AddCommand}" CommandParameter="{Binding }" Foreground="#404040">
                                            <CheckBox.Content>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding }" Visibility="Visible"  TextWrapping="Wrap" MaxWidth="270"/>
                                                </StackPanel>
                                            </CheckBox.Content>
                                        </CheckBox>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

The MainWindow.xaml Code is

<Window x:Class="SampleControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SampleControl" 
        Title="MainWindow" Height="400" Width="525">
    <Grid>
        <local:BTextBox 
            ItemsSource="{Binding Collection}" 
            ProviderCommand="{Binding AutoBTextCommand}" 
            AutoItemsSource="{Binding SuggCollection}" />
    </Grid>
</Window>

The MainWindow.xaml's Code Behind C# Code

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new StringModel();
    }
}

I'm having TWO ViewModels

ViewModel #1 StringModel

class StringModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<string> _collection = new ObservableCollection<string>();
    private ObservableCollection<string> _suggCollection = new ObservableCollection<string>();
    private ObservableCollection<string> _primaryCollection = new ObservableCollection<string>();

    public ObservableCollection<string> Collection
    {
        get { return _collection; }
        set
        {
            _collection = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Collection"));
        }
    }

    public ObservableCollection<string> SuggCollection
    {
        get { return _suggCollection; }
        set
        {
            _suggCollection = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SuggCollection"));
        }
    }

    public StringModel()
    {
        _primaryCollection = new ObservableCollection<string> { 
            "John", "Jack", "James", "Emma", "Peter"
        };
    }

    public ICommand AutoBTextCommand
    {
        get
        {
            return new DelegatingCommand((obj) =>
            {
                Search(obj as string);
            });
        }
    }

    private void Search(string str)
    {
        SuggCollection = new ObservableCollection<string>(_primaryCollection.Where(m => m.ToLowerInvariant().Contains(str.ToLowerInvariant())).Select(m => m));
    }

}

ViewModel #2 IntModel

class IntModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<int> _collection = new ObservableCollection<int>();
    private ObservableCollection<int> _suggCollection = new ObservableCollection<int>();
    private ObservableCollection<int> _primaryCollection = new ObservableCollection<int>();

    public ObservableCollection<int> Collection
    {
        get { return _collection; }
        set
        {
            _collection = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Collection"));
        }
    }

    public ObservableCollection<int> SuggCollection
    {
        get { return _suggCollection; }
        set
        {
            _suggCollection = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SuggCollection"));
        }
    }

    public IntModel()
    {
        _primaryCollection = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                                                11, 12, 13, 14, 16, 17, 18, 19, 20 };
    }

    public ICommand AutoBTextCommand
    {
        get
        {
            return new DelegatingCommand((obj) =>
            {
                Search(obj as string);
            });
        }
    }

    private void Search(string str)
    {
        int item = 0;
        int.TryParse(str, out item);
        SuggCollection = new ObservableCollection<int>(_primaryCollection.Where(m => m == item).Select(m => m));
    }

}

回答1:


First of all, this post would have fitted better in CodeReview.

Second, i can imagine, what you did want to do. To shorten things, i recommend you to not use generic collections in your case.

I've modified the Control a bit:

public class BTextBox : ItemsControl {

    static BTextBox() {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(BTextBox), new FrameworkPropertyMetadata(typeof(BTextBox)));
    }

    private TextBox _textBox;
    private ItemsControl _itemsView;

    public static readonly DependencyProperty ProviderCommandProperty = DependencyProperty.Register("ProviderCommand", typeof(ICommand), typeof(BTextBox), new PropertyMetadata(null));
    public static readonly DependencyProperty AutoItemsSourceProperty = DependencyProperty.Register("AutoItemsSource", typeof(IEnumerable), typeof(BTextBox), new PropertyMetadata(null, OnItemsSourceChanged));

    public IEnumerable AutoItemsSource {
      get {
        return (IEnumerable)GetValue(AutoItemsSourceProperty);
      }
      set {
        SetValue(AutoItemsSourceProperty, value);
      }
    }

    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
      var tb = d as BTextBox;
      if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null)) {
        foreach (var item in e.NewValue as IEnumerable) {
          (tb.AutoItemsSource as IList).Add(item);
        }

      }
    }

    public override void OnApplyTemplate() {
      this._textBox = this.GetTemplateChild("PART_TextBox") as TextBox;
      this._itemsView = this.GetTemplateChild("PART_ListBox_Sugg") as ItemsControl;
      this._itemsView.ItemsSource = this.AutoItemsSource;
      this._textBox.TextChanged += (sender, args) => {
        this.ProviderCommand?.Execute(this._textBox.Text);
      };

      base.OnApplyTemplate();
    }

    public ICommand ProviderCommand {
      get {
        return (ICommand) this.GetValue(ProviderCommandProperty);
      }
      set {
        this.SetValue(ProviderCommandProperty, value);
      }
    }

    public ICommand AddCommand {
      get {
        return new RelayCommand(obj => {
          (this.ItemsSource as IList)?.Add(obj);
        });
      }
    }

  }

Then i've fixed your XAML to get thing to even compile and run:

<Style TargetType="{x:Type local:BTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BTextBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBox x:Name="PART_TextBox" Grid.Row="0"  VerticalAlignment="Center" />
                            <ListBox ItemsSource="{TemplateBinding AutoItemsSource}" Grid.Row="1" x:Name="PART_ListBox_Sugg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BTextBox}}, Path=AddCommand}" CommandParameter="{Binding}" Foreground="#404040">
                                            <CheckBox.Content>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding }" Visibility="Visible"  TextWrapping="Wrap" MaxWidth="270"/>
                                                </StackPanel>
                                            </CheckBox.Content>
                                        </CheckBox>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

At last a valuable remark:

Never ever allow setters on your ItemsSources. If you override them, the binding will break. Use .Clear() and .Add() instead as you see below:

public class StringModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    private readonly ObservableCollection<string> _collection = new ObservableCollection<string>();
    private readonly ObservableCollection<string> _suggCollection = new ObservableCollection<string>();
    private readonly ObservableCollection<string> _primaryCollection = new ObservableCollection<string>();

    public ObservableCollection<string> Collection => this._collection;

    public ObservableCollection<string> SuggCollection => this._suggCollection;

    public StringModel() {
      this._primaryCollection.Add("John");
      this._primaryCollection.Add("Jack");
      this._primaryCollection.Add("James");
      this._primaryCollection.Add("Emma");
      this._primaryCollection.Add("Peter");
    }

    public ICommand AutoBTextCommand {
      get {
        return new RelayCommand(obj => {
          this.Search(obj as string);
        });
      }
    }

    private void Search(string str) {
      this.SuggCollection.Clear();
      foreach (var result in this._primaryCollection.Where(m => m.ToLowerInvariant().Contains(str.ToLowerInvariant())).Select(m => m)) {
        this.SuggCollection.Add(result);
      }

    }

  }

Note

Sice i didnt have your DelegateCommand-implementation, i've used my RelayCommand instead. You can change it withour any issues. I think its the same thing but a different name for it.
You also might consider to display your suggestions right from the start. This might provide a better user-expierience, but thats just my opinion



来源:https://stackoverflow.com/questions/39187879/two-way-binding-issue-of-unknown-object-in-wpf-custom-control-dependency-propert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!