110,534
社区成员
发帖
与我相关
我的任务
分享
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<Style TargetType="{x:Type local:CustomControl3}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl3}">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Name="txt" Width="100" Height="30"/>
<Button Grid.Row="1" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl3}},
Path=XiongJunCommand}" CommandParameter="{Binding ElementName=txt,Path=Text}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:CustomControl3/>
</Grid>
</Window>
public class CustomControl3 : Control
{
static CustomControl3()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl3), new FrameworkPropertyMetadata(typeof(CustomControl3)));
}
public CustomControl3()
{
Loaded += CustomControl3_Loaded;
XiongJunCommand = new RelayCommand<string>((param) => MessageBox.Show("你好" + param.ToString()));
}
private void CustomControl3_Loaded(object sender, RoutedEventArgs e)
{
// XiongJunCommand = new RelayCommand<string>((param) => MessageBox.Show("你好" + param.ToString()));
}
public RelayCommand<string> XiongJunCommand { get; set; }
}