命令 属性 暴露在 UserControl 中不起作用
Command property exposing not working in UserControl
我有一个包含按钮的 UserControl:
<UserControl x:Class="MyProject.Controls.BigButton"
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"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Command="{Binding Command}">
<StackPanel HorizontalAlignment="Center" Margin="5">
<ContentPresenter Width="40" Height="40" Margin="0,0,0,5" Content="{Binding Icon}" />
<TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Text}"></TextBlock>
</StackPanel>
</Button>
</UserControl>
它在代码隐藏文件中公开了一些属性:
public partial class BigButton : UserControl
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BigButton));
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(UIElement), typeof(BigButton));
public UIElement Icon
{
get { return (UIElement)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(BigButton));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public BigButton()
{
InitializeComponent();
}
}
然后我将这个控件添加到视图中是这样的:
<controls:BigButton
Icon="{StaticResource IconStop}"
Text="Stop"
Command="{Binding StopCommand}" />
而且他妈的不执行它的命令。图标和文本都已到位,因此至少一些公开的属性可以正常工作。但命令不是。命令本身似乎没问题,我可以使用
执行它
<Button Command="{Binding StopCommand}" />
所以我想知道,我做错了什么?
默认情况下,您的 BigButton 会继承父级的 DataContext,但您明确将其设置为自身:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
因此当你设置
Command="{Binding StopCommand}"
它开始在 BigButton 的 DataContext 中寻找,因此没有找到应该在输出中反映为错误消息的结果。解决方案是通过设置访问父级的 DataContext:
<Button Command="{Binding Command}" Name="MyButton">
和
public BigButton()
{
InitializeComponent();
this.MyButton.DataContext = this;
}
我建议你参考这个 article。
我有一个包含按钮的 UserControl:
<UserControl x:Class="MyProject.Controls.BigButton"
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"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Command="{Binding Command}">
<StackPanel HorizontalAlignment="Center" Margin="5">
<ContentPresenter Width="40" Height="40" Margin="0,0,0,5" Content="{Binding Icon}" />
<TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Text}"></TextBlock>
</StackPanel>
</Button>
</UserControl>
它在代码隐藏文件中公开了一些属性:
public partial class BigButton : UserControl
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BigButton));
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(UIElement), typeof(BigButton));
public UIElement Icon
{
get { return (UIElement)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(BigButton));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public BigButton()
{
InitializeComponent();
}
}
然后我将这个控件添加到视图中是这样的:
<controls:BigButton
Icon="{StaticResource IconStop}"
Text="Stop"
Command="{Binding StopCommand}" />
而且他妈的不执行它的命令。图标和文本都已到位,因此至少一些公开的属性可以正常工作。但命令不是。命令本身似乎没问题,我可以使用
执行它<Button Command="{Binding StopCommand}" />
所以我想知道,我做错了什么?
默认情况下,您的 BigButton 会继承父级的 DataContext,但您明确将其设置为自身:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
因此当你设置
Command="{Binding StopCommand}"
它开始在 BigButton 的 DataContext 中寻找,因此没有找到应该在输出中反映为错误消息的结果。解决方案是通过设置访问父级的 DataContext:
<Button Command="{Binding Command}" Name="MyButton">
和
public BigButton()
{
InitializeComponent();
this.MyButton.DataContext = this;
}
我建议你参考这个 article。