在用户控件中定义命令绑定

塞巴斯蒂安·布塞克(Sebastian Busek)

我编写了带有2个按钮和一个复选框的用户控件,现在我想将Commands绑定到数据上下文-每个按钮和复选框。但是我不知道如何定义命令绑定。我想我需要在用户控件中使用某种ICommand属性-但是如何连接用户的数据上下文命令委托?我想使用用户控件来管理集合中的每个项目,如下所示:

<ItemsControl ItemsSource="{Binding Path=MoneyInfo}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <local:ChannelSetupControl 
            CurrentCount="{Binding Count}" 
            CoinValue="{Binding Value}"
            UpCommand="{Binding DataContextUp}" 
            DownCommand="{Binding DataContextDown}" 
            ChangeCheckboxCommand="{Binding DataContextChange}"></local:ChannelSetupControl>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

XAML用户控件

<UserControl>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="3*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding CoinValue}" TextAlignment="Center"></TextBlock>

        <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding CurrentCount, Mode=TwoWay}" TextAlignment="Center" VerticalAlignment="Center" FontSize="30"></TextBlock>
        <StackPanel Grid.Column="1" Grid.Row="1" VerticalAlignment="Center">
            <Button Content="+ 10" Padding="0 5"></Button>
            <Button Content="- 10" Padding="0 5"></Button>
        </StackPanel>

        <CheckBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" IsChecked="{Binding Cycling, Mode=TwoWay}" Content="recycling" VerticalContentAlignment="Center"></CheckBox>
    </Grid>
</UserControl>

和后面的代码,这就是我迷路的地方-如何定义UpCommand,DownCommand和ChangeCheckboxCommand?

    public partial class ChannelSetupControl : UserControl, INotifyPropertyChanged
    {
        private int currentCount;
        private bool cycling;
        private double coinValue;

        public int Step { get; set; }

        public double CoinValue { get { return coinValue; } set { coinValue = value; NotifyPropertyChanged("CoinValue"); } }
        public int CurrentCount { get { return currentCount; } set { currentCount = value; NotifyPropertyChanged("CurrentCount"); } }
        public bool Cycling { get { return cycling; } set { cycling = value; NotifyPropertyChanged("Cycling"); } }

        public ChannelSetupControl()
        {
            InitializeComponent();
            DataContext = this;

            CurrentCount = 0;
            Step = 10;
            Cycling = false;
            CoinValue = 0;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
维克

首先,您的ChannelSetupControl类extends UserControl,因此它隐式地扩展了DependencyObjectclass。这意味着您可以使用“依赖项属性”代替实现INotifyPropertyChanged

因此,您可以在ChannelSetupControl类中定义一个依赖项属性,如下所示:

public static readonly DependencyProperty UpCommandProperty =
    DependencyProperty.Register("UpCommand", typeof(ICommand), typeof(ChannelSetupControl));

public ICommand UpCommand
{
    get { return (ICommand)GetValue(UpCommandProperty); }
    set { SetValue(UpCommandProperty, value); }
}

同时在您的控件XAML中:

<Button Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=UpCommand, Mode=OneWay}"
        Content="+ 10" Padding="0 5" />

通过这种方式,您可以在窗口XAML中编写:

<local:ChannelSetupControl UpCommand="{Binding UpCommand, Mode=OneWay}" ... />

您可以对其他控件使用相同的“模式”。关于ICommand,有很多实现。我更喜欢的是所谓的委托命令(有关示例,您可以在此处查看)。我希望这个快速的解释可以对您有所帮助。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在用户控件中绑定ObservableCollection依赖项属性

来自分类Dev

如何在用户控件和父窗口之间绑定WPF命令

来自分类Dev

用户控件中的WPF命令绑定不起作用

来自分类Dev

C# WPF 绑定值不会在用户控件中触发动画

来自分类Dev

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

来自分类Dev

在用户控件中处理对象?

来自分类Dev

在用户控件中的哪里清理?

来自分类Dev

在用户控件中包含CSS文件

来自分类Dev

在用户控件上设置绑定到主窗口的ViewModel

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

WPF用户控件中的命令

来自分类Dev

WPF自定义控件-绑定到在后台代码中定义的命令

来自分类Dev

Vim:在用户定义的命令中发出普通模式命令

来自分类Dev

在用户定义的目录中安装chroot

来自分类Dev

KeyError:在用户定义的函数中

来自分类Dev

使用变量在用户窗体控件中调用值

来自分类Dev

在用户控件中绘制一条简单的线

来自分类Dev

在用户控件MVC中获取空值

来自分类Dev

使用DataTrigger在用户控件中更改图像

来自分类Dev

使用变量在用户窗体控件中调用值

来自分类Dev

在用户控件中的状态之间切换

来自分类Dev

将ItemsControl中的viewmodel绑定到自定义用户控件

来自分类Dev

容器用户控件中的数据绑定

来自分类Dev

容器用户控件中的数据绑定

来自分类Dev

在嵌套用户控件中绑定DependencyProperty

来自分类Dev

将用户控件绑定到itemcontrol中

来自分类Dev

R:在用户定义的Fn中,是否可以合并延迟的命令(可能与用户输入一起使用)?

来自分类Dev

自定义控件事件未在用户控件内触发

Related 相关文章

  1. 1

    在用户控件中绑定ObservableCollection依赖项属性

  2. 2

    如何在用户控件和父窗口之间绑定WPF命令

  3. 3

    用户控件中的WPF命令绑定不起作用

  4. 4

    C# WPF 绑定值不会在用户控件中触发动画

  5. 5

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

  6. 6

    在用户控件中处理对象?

  7. 7

    在用户控件中的哪里清理?

  8. 8

    在用户控件中包含CSS文件

  9. 9

    在用户控件上设置绑定到主窗口的ViewModel

  10. 10

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  11. 11

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  12. 12

    WPF用户控件中的命令

  13. 13

    WPF自定义控件-绑定到在后台代码中定义的命令

  14. 14

    Vim:在用户定义的命令中发出普通模式命令

  15. 15

    在用户定义的目录中安装chroot

  16. 16

    KeyError:在用户定义的函数中

  17. 17

    使用变量在用户窗体控件中调用值

  18. 18

    在用户控件中绘制一条简单的线

  19. 19

    在用户控件MVC中获取空值

  20. 20

    使用DataTrigger在用户控件中更改图像

  21. 21

    使用变量在用户窗体控件中调用值

  22. 22

    在用户控件中的状态之间切换

  23. 23

    将ItemsControl中的viewmodel绑定到自定义用户控件

  24. 24

    容器用户控件中的数据绑定

  25. 25

    容器用户控件中的数据绑定

  26. 26

    在嵌套用户控件中绑定DependencyProperty

  27. 27

    将用户控件绑定到itemcontrol中

  28. 28

    R:在用户定义的Fn中,是否可以合并延迟的命令(可能与用户输入一起使用)?

  29. 29

    自定义控件事件未在用户控件内触发

热门标签

归档