简体   繁体   中英

WPF How to expose fields on a UserControl

I have a simple application with just a window and a user control. The user control has a list box. The user control is positioned on the Window and I want to bind the user control's listbox to an element on the window's data context.

The examples I've been able to find have CLR properties on the user control which are accessed in code not via XAML.

<Window x:Class="WpfApplication2b.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication2b="clr-namespace:WpfApplication2b" Title="MainWindow" Height="410" Width="520">
<Grid>
    <WpfApplication2b:MyUserControl></WpfApplication2b:MyUserControl>
</Grid>

And here is the user control itself.

<UserControl x:Class="WpfApplication2b.MyUserControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="#FFD8AA13">
    <ListBox Height="276" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="276" />
</Grid>

As you can see it's just a listbox on a different coloured background. I have no idea where to go next :)

I'm guessing that I need to add a code behind property for the list box as a dependency property?

Edit: I've added a dependencyProperty, but I don't think I've quite got the point.

    public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty ListBoxProperty;

    static MyUserControl()
    {
        FrameworkPropertyMetadata md = new FrameworkPropertyMetadata();
        MyUserControl.ListBoxProperty = DependencyProperty.Register("MyListBox", typeof (ListBox),
                                                                    typeof (MyUserControl), md);

    }

    public ListBox MyListBox
    {
        get
        {
            return (ListBox) GetValue(ListBoxProperty);
        }
        set
        {
            SetValue(ListBoxProperty, value);
        }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

Your UserControl will inherit the DataContext from the Window so you can bind properties on the ListBox as though it were declared in the Window. To make the control more flexible you can declare Dependency Properties for the data items from the DataContext that you want to use (ie an ItemsSource collection) and pass them into the control, rather than passing the ListBox out .

I think this question/answer is almost what you're looking for. Essentially you're going to need to make a dependency property (using the AddOwner registration method) and set up the DataBinding on the ListBox 's ItemsSource to hook to the Dependency Property. The example in the answer does the same thing for a ComboBox , and should be almost the same for a ListBox .

Exposing inner Control properties for binding in WPF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM