[英]How to get custom property on MainWindow into a UserControl (from UserControl library/DLL)?
I have a set of UserControl
s in a library, but because the library is in a different namespace than the MainWindow
, I don't seem to be able to get one UserControl
to retrieve List<features>
from MainWindow
.我在一个库中有一组
UserControl
,但是因为该库与MainWindow
位于不同的命名空间中,所以我似乎无法获得一个UserControl
来从MainWindow
检索List<features>
。
I suspect this is because UserControl
does not know of MainWindow
, and it's not meant to, as it is in a DLL library.我怀疑这是因为
UserControl
不知道MainWindow
,而且它不是故意的,因为它在 DLL 库中。 As the UserControl
is in a DLL, it should be agnostic to namespaces, but still be able to get what it needs.由于
UserControl
在 DLL 中,它应该与名称空间无关,但仍然能够获得它需要的东西。
So below I put some XAML and relative C# code-behind where you can see on the UserControl
s ListBox
, I'm trying to retrieve the features
list from MainWindow
.所以在下面我把一些 XAML 和相关的 C# 代码隐藏在你可以在
UserControl
s ListBox
上看到的地方,我试图从MainWindow
检索features
列表。
<UserControl x:Class="FlatControls.MyListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FlatControls"
mc:Ignorable="d"
d:DesignHeight="34" d:DesignWidth="100" MaxHeight="34" MaxWidth="100" x:Name="root">
<ListBox x:Name="listBox">
</UserControl>
public MyListBox()
{
InitializeComponent();
listBox.Items = ???????????
}
<Window x:Class="MyApp.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:WpfApp4"
xmlns:FC="clr-namespace:FlatControls;assembly=FlatControls"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1900" WindowState="Maximized" RenderOptions.BitmapScalingMode="Fant">
<Grid>
xyz
</Grid>
<Window>
namespace MyApp
{
public List<string> features = new List<string>();
public MainWindow()
{
InitializeComponent();
features.Add("Concave");
features.Add("Convex");
}
}
Any help would be greatly appreciated, whether it's via Binding, or code-behind:D任何帮助将不胜感激,无论是通过绑定还是代码隐藏:D
The ListBox
in your custom UserControl
has to bind its ItemsSource
from somewhere.自定义
UserControl
中的ListBox
必须从某处绑定其ItemsSource
。 You should create an ItemsSource
dependency property in your MyListBox
to enable binding a collection from outside.您应该在
MyListBox
中创建一个ItemsSource
依赖属性,以启用从外部绑定集合。
public partial class MyListBox : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
nameof(ItemsSource), typeof(IEnumerable), typeof(MyListBox), new PropertyMetadata());
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public MyListBox()
{
InitializeComponent();
}
}
In the markup for the MyListBox
user control, bind to this property using a RelativeSource
binding.在
MyListBox
用户控件的标记中,使用RelativeSource
绑定绑定到此属性。
<UserControl x:Class="FlatControls.MyListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FlatControls"
mc:Ignorable="d"
d:DesignHeight="34" d:DesignWidth="100" MaxHeight="34" MaxWidth="100" x:Name="root">
<ListBox x:Name="listBox" ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBox}}}"/>
</UserControl>
Now, in your MainWindow
use the control (you already added the corresponding XML namespace).现在,在您的
MainWindow
中使用该控件(您已经添加了相应的 XML 命名空间)。
<Window x:Class="MyApp.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:WpfApp4"
xmlns:FC="clr-namespace:FlatControls;assembly=FlatControls"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1900" WindowState="Maximized" RenderOptions.BitmapScalingMode="Fant">
<Grid>
<FC:MyListBox x:Name="MyListBox"/>
</Grid>
<Window>
Finally, assign the ItemsSource
property of the MyListBox
control with the features
collection.最后,将
features
集合分配给MyListBox
控件的ItemsSource
属性。
public MainWindow()
{
InitializeComponent();
features.Add("Concave");
features.Add("Convex");
MyListBox.ItemsSource = Features;
}
Alternatively, expose a property Features
for your items and bind it in XAML.或者,为您的项目公开属性
Features
并将其绑定到 XAML。
public List<string> Features { get; }
<FC:MyListBox ItemsSource="{Binding Features}"/>
As @thatguy said, you can use his way to do.正如@thatguy 所说,您可以使用他的方式来做。 Of course, if you like to write code in view.cs, you can use "FieldModifier" word to decorade you listbox in your MyListBox:
当然,如果你喜欢在 view.cs 中编写代码,你可以使用“FieldModifier”一词来装饰你的 MyListBox 中的列表框:
<ListBox x:Name="listBox" x:FieldModifier="public" />
And in your MainWindow, you can get the listbox instance, of cource, you can set its itemsource like this:在您的 MainWindow 中,您可以获得列表框实例,当然,您可以像这样设置它的 itemsource:
public MainWindow()
{
InitializeComponent();
this.userControl.listBox.ItemsSource = new List<string>() { "11","22","33"};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.