使用WPF C#中的多个控件组合创建自定义控件

我希望创建一个自定义控件,它应该是预定义控件的组合,如Textbox,Button,ListBox等,

请参考以下控件(只是一个样本)

          

我需要在单个自定义控件中组合控件。 当我按下按钮时,我需要在ListItem中添加Textbox值,最后我需要来自此控件的List。

预期的定制控制(只是一个样本)

  

描述 :我需要从用户那里获取字符串列表。 我添加了一个TextBox来获取用户的输入。 我添加了一个Button来在List添加文本。 为了显示List,我添加了一个ListBox。

我需要一个Single控件,它应该inheritance这三个控件。 在那我需要一个ItemsSource 双向绑定 。 如果List已更新,则应更新ItemSource。

我在超过15个地方使用这种结构。 所以,我希望将其作为自定义控件。 请帮助我如何将其作为单一控件实现?

在此处输入图像描述

我不需要用户控件,我需要一个自定义控件,请帮助我……

项目源ViewModel集合甚至不更新 – 尽管ItemsSource具有值。

在此处输入图像描述

我已经为您提供了所需CustomControl的最小示例。

控制

 public class MyCustomControl : ItemsControl { static MyCustomControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl))); } private Button _addButton; private TextBox _textBox; private ListView _itemsView; public override void OnApplyTemplate() { this._addButton = this.GetTemplateChild("PART_AddButton") as Button; this._textBox = this.GetTemplateChild("PART_TextBox") as TextBox; this._itemsView = this.GetTemplateChild("PART_ListBox") as ListView; this._addButton.Click += (sender, args) => { (this.ItemsSource as IList).Add(this._textBox.Text); }; this._itemsView.ItemsSource = this.ItemsSource; base.OnApplyTemplate(); } public ICommand DeleteCommand => new RelayCommand(x => { (this.ItemsSource as IList).Remove((string)x); }); } 

模板

   

RelayCommand

 public class RelayCommand : ICommand { #region Fields private readonly Action _execute; private readonly Predicate _canExecute; #endregion // Fields #region Constructors public RelayCommand(Action execute, Predicate canExecute = null) { if (execute == null) throw new ArgumentNullException(nameof(execute)); this._execute = execute; this._canExecute = canExecute; } #endregion // Constructors #region ICommand Members [DebuggerStepThrough] public bool CanExecute(object parameter) { return this._canExecute == null || this._canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { this._execute(parameter); } #endregion // ICommand Members } 

用法

   

注意请勿使用List作为ItemsSource。 而是使用ObservableCollection因为它自动通知View,你不必处理那个Update-Stuff

干杯

假设这是您的自定义控件:

        

这是您的Parentwindow调用您的自定义控件:

     

你有一个字符串集合,你想用文本框中的文本更新,你可以这样做:在父窗口中将自定义控件的DataContext设置为字符串集合,如下所示:

  public MainWindow() { InitializeComponent(); ObservableCollection stringcollection = new ObservableCollection(); stringcollection.Add("String 1"); stringcollection.Add("String 2"); stringcollection.Add("String 2"); stringcollection.Add("String 3"); customcontrol.DataContext = stringcollection; } 

并在您的自定义控件后退逻辑中添加处理程序到按钮单击事件并执行以下操作:

  private void Button_Click(object sender, RoutedEventArgs e) { var button = sender as Button; var list = button.DataContext as ObservableCollection; list.Add(this.txtbox.Text.ToString()); } 

确保字符串集合是Type Observable Collection,否则每次单击“添加”按钮时列表框都不会更新。

希望能帮助到你。