C# 绑定到单独的DataContext

C# 绑定到单独的DataContext,c#,wpf,C#,Wpf,我有一个主窗口,其中包含一个带有树视图的窗口。树视图绑定到我在DataContext中设置的可观察集合 <TreeView ItemsSource="{Binding Trees}" Name="fileTree" MouseDoubleClick="FileTreeMouseDoubleClick" SelectedValuePath="NodePath"> <TreeView.Reso

我有一个主窗口,其中包含一个带有树视图的窗口。树视图绑定到我在
DataContext
中设置的可观察集合

                    <TreeView ItemsSource="{Binding Trees}"  Name="fileTree" MouseDoubleClick="FileTreeMouseDoubleClick" SelectedValuePath="NodePath">
                        <TreeView.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type local:TreeNodeViewModel}" ItemsSource="{Binding Children}">
                            </HierarchicalDataTemplate>
                        </TreeView.Resources>
                    </TreeView>                        
其中
viewlisttreeviwmodel
main window.xaml.cs
中的成员变量:

private ViewListTreeViewModel viewListTreeViewModel;
它具有以下访问器:

    public ObservableCollection<ViewListTreeNodeViewModel> ViewListTrees
    {
        get { return this.tree; }
    }
我的分层数据模板现在看起来像:

                            <HierarchicalDataTemplate DataType="{x:Type local:ViewListTreeNodeViewModel}" ItemsSource="{Binding Children}">
                                <StackPanel>
                                    <Image Source="{Binding NodeImage}" />
                                    <TextBlock Text="{Binding NodeName}"/>
                                </StackPanel>
                            </HierarchicalDataTemplate>

您可以使用
绑定的
部分直接绑定到对象,或者您可以在
树视图上本地设置另一个
数据上下文

示例1

<TreeView ItemsSource="{Binding Trees, Source=YourOtherDataContext}"/>  
可观察对象

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName)
    {
        return SetValue(ref field, value, propertyName, true);
    }

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <param name="checkForEquality">if set to <c>true</c> [check for equality].</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName, bool checkForEquality)
    {
        if (checkForEquality && EqualityComparer<T>.Default.Equals(field, value))
            return false;

        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, string propertyName)
    {
        return SetValue(setAction, null, propertyName);
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="equalityFunc">The equality func.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, Func<bool> equalityFunc, string propertyName)
    {
        if (equalityFunc != null && !equalityFunc.Invoke())
            return false;

        setAction.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(this, propertyName);
    }

    protected void OnPropertyChanged(object source, string propertyName)
    {
        // copying the event handlers before that this is "thread safe"
        // http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共抽象类ObserveObject:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
/// 
///比较该值并设置它是否已更改。
/// 
/// 
///田野。
///价值。
///属性的名称。
///如果字段已更改,则为True
受保护的虚拟布尔设置值(参考T字段、T值、字符串属性名称)
{
返回SetValue(ref字段,value,propertyName,true);
}
/// 
///比较该值并设置它是否已更改。
/// 
///田野。
///价值。
///属性的名称。
///如果设置为true[检查是否相等]。
///如果字段已更改,则为True
受保护的虚拟布尔SetValue(参考T字段、T值、字符串属性名称、布尔checkForEquality)
{
if(checkForEquality&&EqualityComparer.Default.Equals(字段,值))
返回false;
字段=值;
OnPropertyChanged(propertyName);
返回true;
}
/// 
///设置值。
/// 
/// 
///设定动作。
///属性的名称。
/// 
受保护的虚拟布尔SetValue(操作setAction,字符串propertyName)
{
返回SetValue(setAction,null,propertyName);
}
/// 
///设置值。
/// 
/// 
///设定动作。
///平等职能。
///属性的名称。
/// 
受保护的虚拟布尔SetValue(操作setAction、Func equalityFunc、字符串propertyName)
{
if(equalityFunc!=null&&!equalityFunc.Invoke())
返回false;
setAction.Invoke();
OnPropertyChanged(propertyName);
返回true;
}
受保护的无效OnPropertyChanged(字符串propertyName)
{
OnPropertyChanged(此,propertyName);
}
受保护的void OnPropertyChanged(对象源,字符串propertyName)
{
//在此之前复制事件处理程序是“线程安全的”
// http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}

您可以使用
绑定的
部分直接绑定到对象,或者您可以在
树视图上本地设置另一个
数据上下文

示例1

<TreeView ItemsSource="{Binding Trees, Source=YourOtherDataContext}"/>  
可观察对象

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName)
    {
        return SetValue(ref field, value, propertyName, true);
    }

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <param name="checkForEquality">if set to <c>true</c> [check for equality].</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName, bool checkForEquality)
    {
        if (checkForEquality && EqualityComparer<T>.Default.Equals(field, value))
            return false;

        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, string propertyName)
    {
        return SetValue(setAction, null, propertyName);
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="equalityFunc">The equality func.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, Func<bool> equalityFunc, string propertyName)
    {
        if (equalityFunc != null && !equalityFunc.Invoke())
            return false;

        setAction.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(this, propertyName);
    }

    protected void OnPropertyChanged(object source, string propertyName)
    {
        // copying the event handlers before that this is "thread safe"
        // http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共抽象类ObserveObject:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
/// 
///比较该值并设置它是否已更改。
/// 
/// 
///田野。
///价值。
///属性的名称。
///如果字段已更改,则为True
受保护的虚拟布尔设置值(参考T字段、T值、字符串属性名称)
{
返回SetValue(ref字段,value,propertyName,true);
}
/// 
///比较该值并设置它是否已更改。
/// 
///田野。
///价值。
///属性的名称。
///如果设置为true[检查是否相等]。
///如果字段已更改,则为True
受保护的虚拟布尔SetValue(参考T字段、T值、字符串属性名称、布尔checkForEquality)
{
if(checkForEquality&&EqualityComparer.Default.Equals(字段,值))
返回false;
字段=值;
OnPropertyChanged(propertyName);
返回true;
}
/// 
///设置值。
/// 
/// 
///设定动作。
///属性的名称。
/// 
受保护的虚拟布尔SetValue(操作setAction,字符串propertyName)
{
返回SetValue(setAction,null,propertyName);
}
/// 
///设置值。
/// 
/// 
///设定动作。
///平等职能。
///属性的名称。
/// 
受保护的虚拟布尔SetValue(操作setAction、Func equalityFunc、字符串propertyName)
{
if(equalityFunc!=null&&!equalityFunc.Invoke())
返回false;
setAction.Invoke();
OnPropertyChanged(propertyName);
返回true;
}
受保护的无效OnPropertyChanged(字符串propertyName)
{
OnPropertyChanged(此,propertyName);
}
受保护的void OnPropertyChanged(对象源,字符串propertyName)
{
//在此之前复制事件处理程序是“线程安全的”
// http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}

只需在绑定到窗口的类上公开两个属性(而不是直接绑定集合),公开
ObservableCollection
属性<代码>树
分开树
并相应地绑定每个树视图:

<Window>
  <Grid>
    <TreeView ItemsSource="{Binding Trees}">
      ...
    </TreeView>
    <TreeView ItemsSource="{Binding SeperateTree}">
      ...
    </TreeView>
  </Grid>
</Window>

...
...

只需在绑定到y的类上公开两个属性即可
public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName)
    {
        return SetValue(ref field, value, propertyName, true);
    }

    /// <summary>
    /// Compares the value and sets iff it has changed.
    /// </summary>
    /// <param name="field">The field.</param>
    /// <param name="value">The value.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <param name="checkForEquality">if set to <c>true</c> [check for equality].</param>
    /// <returns><c>True</c> if the field was changed</returns>
    protected virtual bool SetValue<T>(ref T field, T value, string propertyName, bool checkForEquality)
    {
        if (checkForEquality && EqualityComparer<T>.Default.Equals(field, value))
            return false;

        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, string propertyName)
    {
        return SetValue(setAction, null, propertyName);
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="setAction">The set action.</param>
    /// <param name="equalityFunc">The equality func.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <returns></returns>
    protected virtual bool SetValue(Action setAction, Func<bool> equalityFunc, string propertyName)
    {
        if (equalityFunc != null && !equalityFunc.Invoke())
            return false;

        setAction.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(this, propertyName);
    }

    protected void OnPropertyChanged(object source, string propertyName)
    {
        // copying the event handlers before that this is "thread safe"
        // http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
<Window>
  <Grid>
    <TreeView ItemsSource="{Binding Trees}">
      ...
    </TreeView>
    <TreeView ItemsSource="{Binding SeperateTree}">
      ...
    </TreeView>
  </Grid>
</Window>