【发布时间】:2018-05-01 00:31:22
【问题描述】:
我有用户控制:
<UserControl x:Class="MyApp.Header"
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"
d:DesignHeight="40" d:DesignWidth="300" DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}">
<Grid>
<Label Content="{Binding LableContent, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"></Label>
<Button Command="{Binding Path=AddClick, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">
<Image Source="{StaticResource addImage}" Height="20"/>
</Button>
</Grid>
</UserControl>
以及usercontoror中的依赖属性:
public string LableContent
{
get { return (string)GetValue(LableContentProperty); }
set { SetValue(LableContentProperty, value); }
}
public static readonly DependencyProperty LableContentProperty =
DependencyProperty.Register("LableContent", typeof(string), typeof(Header));
public ICommand AddClick
{
get { return (ICommand)GetValue(AddClickProperty); }
set { SetValue(AddClickProperty, value); }
}
public static readonly DependencyProperty AddClickProperty =
DependencyProperty.Register("AddClick", typeof(ICommand), typeof(Header));
我在主窗口上添加了用户控件:
<local:Header AddClick="{Binding Path=AddUser_Click}" LableContent="Users"></local:Header>
并在 MainWindow.cs 上添加点击事件
private void AddUser_Click(object sender, RoutedEventArgs e)
{
}
问题是 Lable 正在填充,但没有调用单击按钮的命令。我做错了什么?
【问题讨论】: