C# 带有子标记的WPF用户控件
我想知道是否有可能创建一个WPF用户控件并拥有子/子用户控件? 如果可能的话,最好的方法是什么,或者从哪里开始 例如:C# 带有子标记的WPF用户控件,c#,wpf,visual-studio-2012,user-controls,C#,Wpf,Visual Studio 2012,User Controls,我想知道是否有可能创建一个WPF用户控件并拥有子/子用户控件? 如果可能的话,最好的方法是什么,或者从哪里开始 例如: <CustomMenu> <CustomMenuItem x:Name="menuItem1" /> <CustomMenuItem x:Name="menuItem2" /> .... </CustomMenu> .... 我的想法是让UserControl获取一些信息和属性,并且它的子对象具
<CustomMenu>
<CustomMenuItem x:Name="menuItem1" />
<CustomMenuItem x:Name="menuItem2" />
....
</CustomMenu>
....
我的想法是让UserControl
获取一些信息和属性,并且它的子对象具有识别它们的其他属性
我曾尝试在父用户控件中创建一个变量,例如UIElementCollection
,并在XAML文件中表示为StackPanel
,但这并不完全是我想要的,因为它允许其他子对象来自不同类型,并且我无法为父对象设置任何属性UserControl
如宽度
我仍然是WPF的初学者,所以我知道我一定会错过,并且不知道很多东西,所以请忍受我
我感谢所有的帮助或指导。
多谢各位
编辑:
在应用了@Dennis()的解决方案后,我注意到一个奇怪的行为,如果我在另一个窗口或页面中有另一个UserControl实例,它将在第二个菜单中累积第一个菜单项。例如:如果我在主页中有一个菜单,在另一页中有另一个菜单,当我打开主页时,它将完美地显示出来,但是如果我打开另一页,我将在第一页中有菜单项,在第二页中有菜单项涅磐牧师您可以通过扩展系统.Windows.Controls.Panel
类创建控件:
示例代码可从以下网站下载:
您可以通过扩展System.Windows.Controls.Panel
类创建控件:
示例代码可从以下网站下载:
您可以通过扩展System.Windows.Controls.Panel
类创建控件:
示例代码可从以下网站下载:
您可以通过扩展System.Windows.Controls.Panel
类创建控件:
示例代码可从以下网站下载:
可以创建WPF UserControl并具有子/子控件
用户控制与否
是的,这是可能的。也许,简短的示例会有所帮助。
父控件(XAML):
父控件(代码隐藏):
public部分类CustomMenu:UserControl
{
公共自定义菜单()
{
初始化组件();
}
公共可观测收集项目
{
get{return(ObservableCollection)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
公共静态只读DependencyProperty项属性=DependencyProperty.Register(
“项目”,
类型(可观测采集),
类型(自定义菜单),
新的PropertyMetadata(新的ObservableCollection());
}
子控件(XAML):
用法:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomMenu>
<local:CustomMenu.Items>
<local:CustomMenuItem />
<local:CustomMenuItem />
<local:CustomMenuItem />
</local:CustomMenu.Items>
</local:CustomMenu>
</Grid>
</Window>
但不清楚的是,你想要实现什么。也许,你需要一个,继承自
可以创建WPF UserControl并具有子/子控件
用户控制与否
是的,这是可能的。也许,简短的示例会有所帮助。
父控件(XAML):
父控件(代码隐藏):
public部分类CustomMenu:UserControl
{
公共自定义菜单()
{
初始化组件();
}
公共可观测收集项目
{
get{return(ObservableCollection)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
公共静态只读DependencyProperty项属性=DependencyProperty.Register(
“项目”,
类型(可观测采集),
类型(自定义菜单),
新的PropertyMetadata(新的ObservableCollection());
}
子控件(XAML):
用法:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomMenu>
<local:CustomMenu.Items>
<local:CustomMenuItem />
<local:CustomMenuItem />
<local:CustomMenuItem />
</local:CustomMenu.Items>
</local:CustomMenu>
</Grid>
</Window>
但不清楚的是,你想要实现什么。也许,你需要一个,继承自
可以创建WPF UserControl并具有子/子控件
用户控制与否
是的,这是可能的。也许,简短的示例会有所帮助。
父控件(XAML):
父控件(代码隐藏):
public部分类CustomMenu:UserControl
{
公共自定义菜单()
{
初始化组件();
}
公共可观测收集项目
{
get{return(ObservableCollection)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
公共静态只读DependencyProperty项属性=DependencyProperty.Register(
“项目”,
类型(可观测采集),
类型(自定义菜单),
新的PropertyMetadata(新的ObservableCollection());
}
子控件(XAML):
用法:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomMenu>
<local:CustomMenu.Items>
<local:CustomMenuItem />
<local:CustomMenuItem />
<local:CustomMenuItem />
</local:CustomMenu.Items>
</local:CustomMenu>
</Grid>
</Window>
但不清楚的是,你想要实现什么。也许,你需要一个,继承自
可以创建WPF UserControl并具有子/子控件
用户控制与否
是的,这是可能的。也许,简短的示例会有所帮助。
父控件(XAML):
父控件(代码隐藏):
public部分类CustomMenu:UserControl
{
公共自定义菜单()
{
初始化组件();
}
公共可观测收集项目
{
get{return(ObservableCollection)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
公共静电
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomMenu>
<local:CustomMenu.Items>
<local:CustomMenuItem />
<local:CustomMenuItem />
<local:CustomMenuItem />
</local:CustomMenu.Items>
</local:CustomMenu>
</Grid>
</Window>