如何将字段绑定到用户控件

时间:2022-09-10 15:51:39

In my user control I have this property:

在我的用户控件中,我有这个属性:

    public static DependencyProperty FooListProperty = DependencyProperty.Register(
        "FooList", typeof(List<Problem>), typeof(ProblemView));

    public List<Problem> FooList
    {
        get
        {
            return (List<Problem>)GetValue(FooListProperty);
        }
        set
        {
            SetValue(FooListProperty, value);
        }
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if (e.Property == FooListProperty)
        {
            // Do something
        }
    }

And since another window, I´m trying to set a value for the last user control:

从另一个窗口开始,我试图为最后一个用户控件设置一个值:

    <local:ProblemView HorizontalAlignment="Center"
                       VerticalAlignment="Center" FooList="{Binding list}" />

And that window in load contains:

负载中的窗口包含:

    public List<Problem> list;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Some processes and it sets to list field
        list = a;
    }

But in XAML code, binding it isn't working. Don't pass the data. What am I wrong?

但在XAML代码中,绑定它是行不通的。不要传递数据。我错了什么?

1 个解决方案

#1


1  

You can't bind to a Field in WPF, you'll have to change list to a property instead.

您无法绑定到WPF中的字段,您必须将列表更改为属性。

You call the Dependency Property FooList in your UserControl and ResultList in Xaml but I'm guessing that's a typo in the question.

您在Xaml中的UserControl和ResultList中调用Dependency Property FooList,但我猜这是问题中的拼写错误。

You should implement INotifyPropertyChanged in the Window to let the Bindings know that the value has been updated.

您应该在窗口中实现INotifyPropertyChanged以让Bindings知道该值已更新。

I'm not sure if you have the correct DataContext set in the Xaml ProblemView, if you're unsure you can name the Window and use ElementName in the binding

我不确定你是否在Xaml ProblemView中设置了正确的DataContext,如果你不确定你可以命名Window并在绑定中使用ElementName

<Window Name="window"
        ...>
    <!--...-->
    <local:ProblemView HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       ResultList="{Binding ElementName=window,
                                            Path=List}" />
    <!--...-->
</Window>

Sample code behind

示例代码背后

public partial class MainWindow : Window, INotifyPropertyChanged
{
    //...

    private List<Problem> m_list;
    public List<Problem> List
    {
        get { return m_list; }
        set
        {
            m_list = value;
            OnPropertyChanged("List");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

#1


1  

You can't bind to a Field in WPF, you'll have to change list to a property instead.

您无法绑定到WPF中的字段,您必须将列表更改为属性。

You call the Dependency Property FooList in your UserControl and ResultList in Xaml but I'm guessing that's a typo in the question.

您在Xaml中的UserControl和ResultList中调用Dependency Property FooList,但我猜这是问题中的拼写错误。

You should implement INotifyPropertyChanged in the Window to let the Bindings know that the value has been updated.

您应该在窗口中实现INotifyPropertyChanged以让Bindings知道该值已更新。

I'm not sure if you have the correct DataContext set in the Xaml ProblemView, if you're unsure you can name the Window and use ElementName in the binding

我不确定你是否在Xaml ProblemView中设置了正确的DataContext,如果你不确定你可以命名Window并在绑定中使用ElementName

<Window Name="window"
        ...>
    <!--...-->
    <local:ProblemView HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       ResultList="{Binding ElementName=window,
                                            Path=List}" />
    <!--...-->
</Window>

Sample code behind

示例代码背后

public partial class MainWindow : Window, INotifyPropertyChanged
{
    //...

    private List<Problem> m_list;
    public List<Problem> List
    {
        get { return m_list; }
        set
        {
            m_list = value;
            OnPropertyChanged("List");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}