简体   繁体   English

WPF用户控件未绑定数据

[英]WPF User Control Not Data Binding

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: 到目前为止,我的代码:

My user control is basically a enhanced combo box. 我的用户控件基本上是一个增强的组合框。 The DP lives inside of the user control. DP位于用户控件内部。 I am using this user control inside of a Data Grid. 我在数据网格内使用此用户控件。

The data context of the user control is set like this. 这样设置用户控件的数据上下文。 DataContext={Binding RelativeSource={RelativeSource Self}} 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

In my xaml file I have the following: 在我的xaml文件中,我具有以下内容:

<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>

The DataGrid is bound to an ObservableCollection. DataGrid绑定到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. 如果有任何专业提示,请告诉我。

The data context of the user control is set like this. 这样设置用户控件的数据上下文。 DataContext={Binding RelativeSource={RelativeSource Self}}

Remove that. 删除它。 Explicitly setting a UserControl's DataContext prevents inheriting a DataContext from the control's parent, eg here: 显式设置UserControl的DataContext可防止从控件的父级继承DataContext,例如:

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

The bindings are expected to be resolved against the current DataContext, ie a view model object with a NewProfile and Type property. 预期将针对当前DataContext解析绑定,即具有NewProfileType属性的视图模型对象。 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. 因此,只是不要显式设置UserControl的DataContext。 Never. 决不。 Any blogs or online tutorials telling you so are plain wrong. 任何告诉您的博客或在线教程都是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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