[英]Create a Custom Control with the combination of multiple controls in WPF C#
我想創建一個自定義控件,它應該是預定義控件的組合,如Textbox,Button,ListBox等,
請參考以下控件(只是一個樣本)
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" />
<Button Grid.Column="1" Content="Add" Margin="20,0" />
</Grid>
<ListBox ItemsSource="{Binding textBox}" Grid.Row="1" Margin="0,25">
<ListBoxItem />
</ListBox>
</Grid>
我需要在單個自定義控件中組合控件。 當我按下按鈕時,我需要在ListItem中添加Textbox值,最后我需要來自此控件的List。
預期的定制控制(只是一個樣本)
<cust:MultiControl ItemsSource="{Binding stringCollection}" />
描述 :我需要從用戶那里獲取字符串列表。 我添加了一個TextBox來獲取用戶的輸入。 我添加了一個Button來在List<string>
添加文本。 為了顯示List,我添加了一個ListBox。
我需要一個Single控件,它應該繼承這三個控件。 在那我需要一個ItemsSource
雙向綁定 。 如果List<string>
已更新,則應更新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<string>).Add(this._textBox.Text);
};
this._itemsView.ItemsSource = this.ItemsSource;
base.OnApplyTemplate();
}
public ICommand DeleteCommand => new RelayCommand(x => { (this.ItemsSource as IList<string>).Remove((string)x); });
}
模板
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBox x:Name="PART_TextBox" Grid.Column="0" />
<Button x:Name="PART_AddButton" Grid.Column="1" Content="Add" Margin="20,0" />
</Grid>
<ListView ItemsSource="{TemplateBinding ItemsSource}" Grid.Row="1" Margin="0,25" x:Name="PART_ListBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding}"/>
<Button Content="X" Foreground="Red"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyCustomControl}}, Path=DeleteCommand}"
CommandParameter="{Binding}" Margin="10,0,0,0"></Button>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
RelayCommand
public class RelayCommand : ICommand
{
#region Fields
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute, Predicate<object> 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
}
用法
<local:MyCustomControl ItemsSource="{Binding Collection}"/>
注意請勿使用List
作為ItemsSource。 而是使用ObservableCollection
因為它自動通知View,你不必處理那個Update-Stuff
干杯
假設這是您的自定義控件:
<UserControl x:Class="CustomControl.UserControl1"
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:CustomControl"
mc:Ignorable="d" >
<StackPanel Width="200" Margin="15">
<TextBox Name="txtbox"/>
<Button Content="Add"
Margin="20,0" Click="Button_Click"/>
<ListBox ItemsSource="{Binding}"
Margin="0,25">
</ListBox>
</StackPanel>
這是您的Parentwindow調用您的自定義控件:
<Window x:Class="ParentControl.MainWindow"
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:ParentControl"
mc:Ignorable="d"
xmlns:customcontrol="clr-namespace:CustomControl;assembly=CustomControl"
Title="MainWindow" Height="350" Width="525">
<Grid>
<customcontrol:UserControl1 Name="customcontrol"></customcontrol:UserControl1>
</Grid>
你有一個字符串集合,你想用文本框中的文本更新,你可以這樣做:在父窗口中將自定義控件的DataContext設置為字符串集合,如下所示:
public MainWindow()
{
InitializeComponent();
ObservableCollection<string> stringcollection = new ObservableCollection<string>();
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<string>;
list.Add(this.txtbox.Text.ToString());
}
確保字符串集合是Type Observable Collection,否則每次單擊“添加”按鈕時列表框都不會更新。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.