WPF自定义控件和直接内容支持
我是WPF的新手,有点困了,所以任何帮助都将不胜感激 我正在尝试编写WPF自定义控件,它封装了我已经在使用的功能的几个元素(即排序、筛选、标准菜单等),但是封装在一个漂亮整洁的包中以避免重复 无论如何,我已经创建了自定义控件(基于控件),然后在Generic.Xaml中有以下内容WPF自定义控件和直接内容支持,wpf,wpf-controls,Wpf,Wpf Controls,我是WPF的新手,有点困了,所以任何帮助都将不胜感激 我正在尝试编写WPF自定义控件,它封装了我已经在使用的功能的几个元素(即排序、筛选、标准菜单等),但是封装在一个漂亮整洁的包中以避免重复 无论如何,我已经创建了自定义控件(基于控件),然后在Generic.Xaml中有以下内容 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="htt
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Controls.ListViewExtended">
<Style TargetType="{x:Type local:ListViewExtended}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ListViewExtended}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ListView>
<ListView.View>
<GridView>
<!-- Content goes here -->
</GridView>
</ListView.View>
</ListView>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
提前感谢从
列表视图
继承自定义控件,而不是从控件
继承。这肯定会导致您更改模板,但我鼓励您阅读更多关于如何更改模板的文档(例如Sacha Barber的文章:)
祝你学习顺利 从
列表视图继承自定义控件,而不是从控件继承。这肯定会导致您更改模板,但我鼓励您阅读更多关于如何更改模板的文档(例如Sacha Barber的文章:)
祝你学习顺利 我在我们的项目中使用这个简单的解决方案来支持自定义控件中的直接内容:
将“CustomControl”添加到项目中,并从类“UserControl”而不是“control”派生此控件:
当您向项目中添加CustomControl时,Visual Studio(我正在使用2012)会自动添加一个文件夹“Themes”,其中包括一个名为“Generic.xaml”的文件。
此文件包含一个ResourceDictionary,用于定义CustomControl的样式(模板)
您将找到CustomControl的基本模板,该模板已用作DefaultStyle。要获得直接内容支持,请在此模板中的某个位置放置ContentPresenter,并使用父内容绑定:
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在可以向CustomControl添加内容:
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MyApplication"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:MyCustomControl>
<TextBlock>Hello</TextBlock>
</controls:MyCustomControl>
</Grid>
</Window>
你好
希望这有帮助 我在我们的项目中使用这个简单的解决方案来支持自定义控件中的直接内容:
将“CustomControl”添加到项目中,并从类“UserControl”而不是“control”派生此控件:
当您向项目中添加CustomControl时,Visual Studio(我正在使用2012)会自动添加一个文件夹“Themes”,其中包括一个名为“Generic.xaml”的文件。
此文件包含一个ResourceDictionary,用于定义CustomControl的样式(模板)
您将找到CustomControl的基本模板,该模板已用作DefaultStyle。要获得直接内容支持,请在此模板中的某个位置放置ContentPresenter,并使用父内容绑定:
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在可以向CustomControl添加内容:
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MyApplication"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:MyCustomControl>
<TextBlock>Hello</TextBlock>
</controls:MyCustomControl>
</Grid>
</Window>
你好
希望这有帮助 您只需指定ContentPropertyAttribute
[ContentProperty("MainContent")]
public class GroupPanel : Control
{
public GroupPanel()
{
DefaultStyleKey = typeof(GroupPanel);
}
public object MainContent
{
get { return GetValue(MainContentProperty); }
set { SetValue(MainContentProperty, value); }
}
public static readonly DependencyProperty MainContentProperty =
DependencyProperty.Register("MainContent", typeof(object), typeof(GroupPanel), null);
}
参考:您只需指定ContentPropertyAttribute
[ContentProperty("MainContent")]
public class GroupPanel : Control
{
public GroupPanel()
{
DefaultStyleKey = typeof(GroupPanel);
}
public object MainContent
{
get { return GetValue(MainContentProperty); }
set { SetValue(MainContentProperty, value); }
}
public static readonly DependencyProperty MainContentProperty =
DependencyProperty.Register("MainContent", typeof(object), typeof(GroupPanel), null);
}
参考资料:不要使用
{Binding MainContent ElementName=Self}
使用
这样可以节省我很多时间 不要使用
{Binding MainContent ElementName=Self}
使用
这样可以节省我很多时间 我已经试过了,它很有效,我得到了一个我可以使用的列表视图。但是,我希望能够将样式和弹出菜单添加到控件中,而不是在控件的每个瞬间?然而,对我来说,这听起来像是一个不同的问题。我已经尝试过了,它很有效,我得到了一个我可以使用的列表视图。但是,我希望能够将样式和弹出菜单添加到控件中,而不是在控件的每个瞬间?然而,对我来说,这听起来像是一个不同的问题。解释为什么这样做会让你的答案对未来的读者更有价值。解释为什么这样做会让你的答案对未来的读者更有价值。