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

巴拉曼尼甘丹

我希望创建一个自定义控件,它应该是预定义控件(如文本框,按钮,列表框等)的组合,

请参考以下控件(仅作为示例)

<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>

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

预期的自定义控件(仅作为示例)

<cust:MultiControl ItemsSource="{Binding stringCollection}" />

说明:我需要从用户那里获取字符串列表。我添加了一个TextBox来从User获取输入。我添加了一个按钮,以将文本添加到中List<string>为了显示列表,我添加了一个ListBox。

我需要一个控件,它应该继承这三个控件。在我需要一个ItemsSource用于双向结合如果List<string>已更新,则应更新ItemSource。

我在15个以上的地方使用了这种结构。因此,我希望将其作为“自定义”控件。请协助我如何将其实现为单个控件?

在此处输入图片说明

我不需要用户控件,我需要自定义控件,请帮助我...

尽管It​​emsSource具有价值,但Item Source ViewModel集合不会更新。

在此处输入图片说明

oku

我为您提供了所需的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>

中继命令

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}"/>

注意不要使用aList作为您的ItemsSource。而是使用一个,ObservableCollection因为它会自动通知View,而您不必处理该Update-Stuff

干杯

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以创建一个使用Generic.xml中的另一个自定义控件的自定义控件吗

来自分类Dev

如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

来自分类Dev

如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

来自分类Dev

在 C# 中创建从 WPF 控件派生的自定义 <Page> 类?

来自分类Dev

WPF:自定义控件属性已被另一个自定义控件错误注册

来自分类Dev

WPF:自定义创建的用户控件中的数据绑定

来自分类Dev

如何:创建一个GridSplitter,它可以自定义DockPanel的大小(C#,WPF)

来自分类Dev

使用DataBinding创建自定义控件

来自分类Dev

WPF C#Databind:在C#中,突出显示另一个控件时更改控件颜色

来自分类Dev

WPF C#Databind:在C#中,突出显示另一个控件时更改控件颜色

来自分类Dev

wpf-无法使用自定义控件

来自分类Dev

WPF自定义控件不使用ToggleButton的默认样式

来自分类Dev

最佳WPF面板可创建自定义控件

来自分类Dev

如何使用Elmish.wpf和F#管理多个窗口,用户控件和自定义控件?

来自分类Dev

如何使用分段控件在一个表视图中维护两个自定义单元格?

来自分类Dev

C# WPF - 发生另一个控件事件时更改控件内容

来自分类Dev

WPF中的自定义控件模板的Click事件

来自分类Dev

WPF自定义控件中的依赖关系

来自分类Dev

WPF:如何使自定义控件中的文本显示?

来自分类Dev

WPF-更新自定义控件中的值

来自分类Dev

在WPF MaterialDesign中悬停自定义控件

来自分类Dev

WPF KeyBinding 命令未在自定义控件中触发

来自分类Dev

自定义 WPF 控件的好处

来自分类Dev

WPF 样式自定义控件

来自分类Dev

WPF中的自定义控件是否与Winforms中一样?

来自分类Dev

子控件未继承 WPF 自定义控件样式

来自分类Dev

有没有办法使用 c#(Windows 窗体)从设计器访问自定义用户控件中的控件属性

来自分类Dev

使用WPF 4.5中的INotifyDataErrorInfo创建一个显示控件的所有Validation.Error的工具提示

来自分类Dev

使用Storyboard XAML创建自定义控件

Related 相关文章

  1. 1

    我可以创建一个使用Generic.xml中的另一个自定义控件的自定义控件吗

  2. 2

    如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

  3. 3

    如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

  4. 4

    在 C# 中创建从 WPF 控件派生的自定义 <Page> 类?

  5. 5

    WPF:自定义控件属性已被另一个自定义控件错误注册

  6. 6

    WPF:自定义创建的用户控件中的数据绑定

  7. 7

    如何:创建一个GridSplitter,它可以自定义DockPanel的大小(C#,WPF)

  8. 8

    使用DataBinding创建自定义控件

  9. 9

    WPF C#Databind:在C#中,突出显示另一个控件时更改控件颜色

  10. 10

    WPF C#Databind:在C#中,突出显示另一个控件时更改控件颜色

  11. 11

    wpf-无法使用自定义控件

  12. 12

    WPF自定义控件不使用ToggleButton的默认样式

  13. 13

    最佳WPF面板可创建自定义控件

  14. 14

    如何使用Elmish.wpf和F#管理多个窗口,用户控件和自定义控件?

  15. 15

    如何使用分段控件在一个表视图中维护两个自定义单元格?

  16. 16

    C# WPF - 发生另一个控件事件时更改控件内容

  17. 17

    WPF中的自定义控件模板的Click事件

  18. 18

    WPF自定义控件中的依赖关系

  19. 19

    WPF:如何使自定义控件中的文本显示?

  20. 20

    WPF-更新自定义控件中的值

  21. 21

    在WPF MaterialDesign中悬停自定义控件

  22. 22

    WPF KeyBinding 命令未在自定义控件中触发

  23. 23

    自定义 WPF 控件的好处

  24. 24

    WPF 样式自定义控件

  25. 25

    WPF中的自定义控件是否与Winforms中一样?

  26. 26

    子控件未继承 WPF 自定义控件样式

  27. 27

    有没有办法使用 c#(Windows 窗体)从设计器访问自定义用户控件中的控件属性

  28. 28

    使用WPF 4.5中的INotifyDataErrorInfo创建一个显示控件的所有Validation.Error的工具提示

  29. 29

    使用Storyboard XAML创建自定义控件

热门标签

归档