WPF用户控件未绑定数据 [英] WPF User Control Not Data Binding

查看:71
本文介绍了WPF用户控件未绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户控件中,我具有一个非数据绑定的依赖项属性。我查看了几个堆栈溢出的帖子,但无法弄清楚自己在做什么错。从未调用过属性更改方法。到目前为止,我的代码:

In my user control I have a dependency property that is not data binding. I have looked over several stack overflow posts and I cannot figure out what I am doing wrong. The Property changed method is never called. My Code so far:

我的用户控件基本上是增强的组合框。 DP位于用户控件内部。我在数据网格内部使用此用户控件。

My user control is basically a enhanced combo box. The DP lives inside of the user control. I am using this user control inside of a Data Grid.

用户控件的数据上下文设置如下。
DataContext = {Binding RelativeSource = {RelativeSource Self}}

The data context of the user control is set like this. DataContext={Binding RelativeSource={RelativeSource Self}}

#region ProfileType DP

    public static FrameworkPropertyMetadata ProfileTypeMetaData = new FrameworkPropertyMetadata(ProfileTypes.Default,
                                                                 FrameworkPropertyMetadataOptions.BindsTwoWayByDefault |
                                                                FrameworkPropertyMetadataOptions.Journal, new PropertyChangedCallback(ProfileType_PropertyChanged),
                                                                new CoerceValueCallback(ProfileType_CoerceValue),
                                                                false, UpdateSourceTrigger.PropertyChanged);

    public static readonly DependencyProperty ProfileTypeProperty = DependencyProperty.Register(nameof(ProfileType), typeof(ProfileTypes),
                                                                  typeof(MyClass), ProfileTypeMetaData, new ValidateValueCallback(ProfileType_Validate));

    private static void ProfileType_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
    {
        MyClass tp = (MyClass)dobj;
        tp.SetUpProfiles();
    }

    private static object ProfileType_CoerceValue(DependencyObject dobj, object Value)
    {
        return Value;
    }

    private static bool ProfileType_Validate(object Value)
    {
        return true;
    }

    public ProfileTypes ProfileType
    {
        get
        {
            return (ProfileTypes)this.GetValue(ProfileTypeProperty);
        }
        set
        {
            this.SetValue(ProfileTypeProperty, value);
        }
    }

    #endregion

在我的xaml文件具有以下内容:

In my xaml file I have the following:

<DataGrid ItemsSource="{Binding Missmatches}" CanUserAddRows="False" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Target Profile" Binding="{Binding OldProfile}" />
          <DataGridTemplateColumn Header="Mismatched Profile">
             <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Tekla:TeklaProfiles SelectedProfile="{Binding NewProfile}" ProfileType="{Binding Type}" />
                  </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

DataGrid绑定到ObservableCollection。集合中的每个对象都包含三个自动道具。

The DataGrid is bound to an ObservableCollection. Each object in the collection contains three auto props.

private ObservableCollection<ProfileMismatch> _missmatches;
        public ObservableCollection<ProfileMismatch> Missmatches
        {
            get { return _missmatches; }
            set
            {
                if (_missmatches == value)
                    return;
                _missmatches = value;
                RaisePropertyChanged(nameof(Missmatches));
            }
        }

无论我尝试什么,我似乎都无法用户控件进行数据绑定。

No matter what I try I cannot seem to get the user control to data bind. If any one has some pro tips please let me know.

推荐答案


用户的数据上下文这样设置控件。
DataContext = {Binding RelativeSource = {RelativeSource Self}}

将其删除。明确设置UserControl的DataContext可防止从控件的父级继承DataContext,例如此处:

Remove that. Explicitly setting a UserControl's DataContext prevents inheriting a DataContext from the control's parent, e.g. here:

<DataTemplate>
    <Tekla:TeklaProfiles SelectedProfile="{Binding NewProfile}"
                         ProfileType="{Binding Type}" />
</DataTemplate>

预计将针对当前DataContext解析绑定,即具有<$的视图模型对象c $ c> NewProfile 和 Type 属性。但是,由于您已经明确地将UserControl的DataContext设置为自身,因此它将无法正常工作。

The bindings are expected to be resolved against the current DataContext, i.e. a view model object with a NewProfile and Type property. However, since you've explicitly set the DataContext of the UserControl (to itself), it won't work.

因此,只是不要显式设置UserControl的DataContext。决不。任何告诉您的博客或在线教程都是错误的。

So just don't explicitly set a UserControl's DataContext. Never. Any blogs or online tutorials telling you so are plain wrong.

这篇关于WPF用户控件未绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆