I found something about this issue for ASP, but it didn't help me much ...
我在ASP中发现了一些关于这个问题的东西,但它对我帮助不大......
What I'd like to do is the following: I want to create a user control that has a collection as property and buttons to navigate through this collection. I want to be able to bind this user control to a collection and display different controls on it (containing data from that collection). Like what you had in MS Access on the lower edge of a form ...
我想要做的是以下内容:我想创建一个用户控件,它具有一个集合作为属性和按钮来浏览此集合。我希望能够将此用户控件绑定到一个集合并在其上显示不同的控件(包含该集合中的数据)。就像你在表格下边缘的MS Access中所拥有的一样......
to be more precise:
更确切地说:
When I actually use the control in my application (after I created it), I want to be able to add multiple controls to it (textboxes, labels etc) between the <myControly>
and </mycontrol>
If I do that now, the controls on my user control disappear.
当我在我的应用程序中实际使用该控件时(在我创建它之后),我希望能够在
4 个解决方案
#1
8
Here is an example of one way to do what you want:
以下是一种实现您想要的方法的示例:
First, the code - UserControl1.xaml.cs
首先,代码 - UserControl1.xaml.cs
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
public object MyContent
{
get { return GetValue(MyContentProperty); }
set { SetValue(MyContentProperty, value); }
}
}
And the user control's XAML - UserControl1.xaml
而用户控件的XAML - UserControl1.xaml
<UserControl x:Class="InCtrl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Name="MyCtrl">
<StackPanel>
<Button Content="Up"/>
<ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/>
<Button Content="Down"/>
</StackPanel>
</UserControl>
And finally, the xaml to use our wonderful new control:
最后,xaml使用我们精彩的新控件:
<Window x:Class="InCtrl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:InCtrl"
Title="Window1" Height="300" Width="300">
<Grid>
<me:UserControl1>
<me:UserControl1.MyContent>
<Button Content="Middle"/>
</me:UserControl1.MyContent>
</me:UserControl1>
</Grid>
</Window>
#2
1
I'm having a hard time understanding your question, but I think what you're describing is an ItemsControl
using DataTemplates
to display the contents of (presumably) an ObservableCollection(T).
我很难理解你的问题,但我认为你所描述的是一个使用DataTemplates的ItemsControl来显示(可能是)ObservableCollection(T)的内容。
#3
0
A UserControl may not be the best way to do this. You're wanting to add decorations around content, which is basically what Border does: it has a child element, and it adds its own stuff around the edges.
UserControl可能不是最好的方法。你想要在内容周围添加装饰,这基本上就是Border所做的:它有一个子元素,它在边缘附加了自己的东西。
Look into the Decorator class, which Border descends from. If you make your own Border descendant, you should be easily able to do what you want. However, I believe this would require writing code, not XAML.
查看Decorator类,它是Border的后代。如果你自己制作边境后裔,你应该可以轻松地做你想做的事。但是,我认为这需要编写代码,而不是XAML。
You might still want to make a UserControl to wrap the buttons at the bottom, just so you can use the visual designer for part of the process. But Decorator would be a good way to glue the pieces together and allow for user-definable content.
您可能仍希望创建一个UserControl来将按钮包装在底部,这样您就可以使用可视化设计器来完成部分过程。但是Decorator是将各个部分粘合在一起并允许用户可定义内容的好方法。
#4
0
Here's a link to a built-in control (HeaderedContentControl
) that does the same thing as the accepted answer except that it is an existing control in WPF since .Net 3.0
这是一个内置控件(HeaderedContentControl)的链接,它与接受的答案做同样的事情,除了它是WPF中的现有控件,因为.Net 3.0
#1
8
Here is an example of one way to do what you want:
以下是一种实现您想要的方法的示例:
First, the code - UserControl1.xaml.cs
首先,代码 - UserControl1.xaml.cs
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
public object MyContent
{
get { return GetValue(MyContentProperty); }
set { SetValue(MyContentProperty, value); }
}
}
And the user control's XAML - UserControl1.xaml
而用户控件的XAML - UserControl1.xaml
<UserControl x:Class="InCtrl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Name="MyCtrl">
<StackPanel>
<Button Content="Up"/>
<ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/>
<Button Content="Down"/>
</StackPanel>
</UserControl>
And finally, the xaml to use our wonderful new control:
最后,xaml使用我们精彩的新控件:
<Window x:Class="InCtrl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:InCtrl"
Title="Window1" Height="300" Width="300">
<Grid>
<me:UserControl1>
<me:UserControl1.MyContent>
<Button Content="Middle"/>
</me:UserControl1.MyContent>
</me:UserControl1>
</Grid>
</Window>
#2
1
I'm having a hard time understanding your question, but I think what you're describing is an ItemsControl
using DataTemplates
to display the contents of (presumably) an ObservableCollection(T).
我很难理解你的问题,但我认为你所描述的是一个使用DataTemplates的ItemsControl来显示(可能是)ObservableCollection(T)的内容。
#3
0
A UserControl may not be the best way to do this. You're wanting to add decorations around content, which is basically what Border does: it has a child element, and it adds its own stuff around the edges.
UserControl可能不是最好的方法。你想要在内容周围添加装饰,这基本上就是Border所做的:它有一个子元素,它在边缘附加了自己的东西。
Look into the Decorator class, which Border descends from. If you make your own Border descendant, you should be easily able to do what you want. However, I believe this would require writing code, not XAML.
查看Decorator类,它是Border的后代。如果你自己制作边境后裔,你应该可以轻松地做你想做的事。但是,我认为这需要编写代码,而不是XAML。
You might still want to make a UserControl to wrap the buttons at the bottom, just so you can use the visual designer for part of the process. But Decorator would be a good way to glue the pieces together and allow for user-definable content.
您可能仍希望创建一个UserControl来将按钮包装在底部,这样您就可以使用可视化设计器来完成部分过程。但是Decorator是将各个部分粘合在一起并允许用户可定义内容的好方法。
#4
0
Here's a link to a built-in control (HeaderedContentControl
) that does the same thing as the accepted answer except that it is an existing control in WPF since .Net 3.0
这是一个内置控件(HeaderedContentControl)的链接,它与接受的答案做同样的事情,除了它是WPF中的现有控件,因为.Net 3.0