Rya*_*yan 90 c# wpf xaml user-controls controls
我有一组带有附加命令和逻辑的控件,它们以相同的方式不断重用.我决定创建一个包含所有常用控件和逻辑的用户控件.
但是我还需要控件才能保存可以命名的内容.我尝试了以下方法:
<UserControl.ContentTemplate>
<DataTemplate>
<Button>a reused button</Button>
<ContentPresenter Content="{TemplateBinding Content}"/>
<Button>a reused button</Button>
</DataTemplate>
</UserControl.ContentTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,似乎无法命名放置在用户控件内的任何内容.例如,如果我以下列方式使用控件:
<lib:UserControl1>
<Button Name="buttonName">content</Button>
</lib:UserControl1>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法在元素"Button"上设置Name属性值"buttonName".'Button'属于元素'UserControl1'的范围,在另一个范围内定义时,已经注册了一个名称.
如果我删除buttonName,然后编译,但我需要能够命名内容.我怎样才能做到这一点?
Syb*_*and 41
答案是不使用UserControl来执行此操作.
创建一个扩展ContentControl的类
public class MyFunkyControl : ContentControl
{
public static readonly DependencyProperty HeadingProperty =
DependencyProperty.Register("Heading", typeof(string),
typeof(HeadingContainer), new PropertyMetadata(HeadingChanged));
private static void HeadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((HeadingContainer) d).Heading = e.NewValue as string;
}
public string Heading { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后使用样式指定内容
<Style TargetType="control:MyFunkyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="control:MyFunkyContainer">
<Grid>
<ContentControl Content="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
最后 - 使用它
<control:MyFunkyControl Heading="Some heading!">
<Label Name="WithAName">Some cool content</Label>
</control:MyFunkyControl>
Run Code Online (Sandbox Code Playgroud)
Rya*_*yan 18
使用XAML时似乎无法做到这一点.当我实际拥有我需要的所有控件时,自定义控件似乎是一种矫枉过正,但只需要将它们与一小部分逻辑组合在一起并允许命名内容.
作为mackenir的JD博客解决方案表明,似乎有最好的妥协.扩展JD的解决方案以允许仍然在XAML中定义控件的方法可以如下:
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
var grid = new Grid();
var content = new ContentPresenter
{
Content = Content
};
var userControl = new UserControlDefinedInXAML();
userControl.aStackPanel.Children.Add(content);
grid.Children.Add(userControl);
Content = grid;
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我创建了一个名为UserControlDefinedInXAML的用户控件,它与使用XAML的任何普通用户控件一样定义.在我的UserControlDefinedInXAML中,我有一个名为aStackPanel的StackPanel,我希望在其中显示我的命名内容.