首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF自定义控件依赖属性中未知对象的双向绑定问题

WPF自定义控件依赖属性中未知对象的双向绑定问题
EN

Stack Overflow用户
提问于 2016-08-28 03:58:43
回答 1查看 848关注 0票数 2

我有一个为AutoComplete TextBox实现的自定义控件。我从下面的问题Create a Custom Control with the combination of multiple controls in WPF C#中得到了所有的想法。在那个自定义控件中,他们建议使用以下代码来添加项,它的完美工作和双向绑定太

代码语言:javascript
运行
复制
(this.ItemsSource as IList<string>).Add(this._textBox.Text);

但是,我将下面的代码更改为未知对象,因此我将IList<string>更改为IList<object>

代码语言:javascript
运行
复制
(this.ItemsSource as IList<object>).Add(item);

XAML:

代码语言:javascript
运行
复制
 <local:BTextBox 
            ItemsSource="{Binding Collection}" 
            ProviderCommand="{Binding AutoBTextCommand}" 
            AutoItemsSource="{Binding SuggCollection}" />

但是它没有更新Collection.的ViewModel属性我也尝试过在xaml中进行以下更改

代码语言:javascript
运行
复制
ItemsSource="{Binding Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

我不知道我在哪里犯的错。

Functionality:-- CustomControl内部的TextBox从用户那里获取输入,它触发命令根据用户输入过滤远程数据的ProviderCommand,,并通过AutoItemsSource,发送过滤的集合--该属性被绑定为CustomControl中的ListBoxItemsSource以选择项。我们可以从AddCommand项中选择项,通过单击该项,它将触发位于CustomControl类中的命令CustomControl,它在CustomControlItemSource属性中添加所选的项。我在这个属性ItemsSource.中有双向绑定问题从这个属性中,我们可以获得所选的项作为集合。

这是我的完整源代码

自定义控件C#代码:

代码语言:javascript
运行
复制
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
}

Generic.xaml代码是

代码语言:javascript
运行
复制
<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>

MainWindow.xaml代码是

代码语言:javascript
运行
复制
<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>

代码背后的MainWindow.xaml代码

代码语言:javascript
运行
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new StringModel();
    }
}

我有两个ViewModels

StringModel ViewModel #1

代码语言:javascript
运行
复制
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));
    }

}

IntModel ViewModel #2

代码语言:javascript
运行
复制
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));
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-30 12:48:53

首先,这篇文章在CodeReview中会更贴切。

第二,我可以想象,你想做什么。为了缩短时间,我建议您使用而不是在您的情况下使用泛型集合。

我对控制做了一点修改:

代码语言:javascript
运行
复制
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);
        });
      }
    }

  }

然后,我修复了您的XAML,使其能够编译和运行:

代码语言:javascript
运行
复制
<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>

最后,一句有价值的话:

永远不要允许设置在您的ItemsSources上。如果覆盖它们,绑定就会中断。使用.Clear().Add()代替如下所示:

代码语言:javascript
运行
复制
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

我没有你的DelegateCommand-implementation,我用了我的RelayCommand。你可以用我们的任何问题改变它。我认为这是同一件事,但它的名字不同。

你也可以考虑从一开始就展示你的建议。这可能会提供更好的用户解释,但这只是我的意见

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39187879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档