绑定到DataContext无法正常工作 [英] Binding to DataContext not Working

查看:82
本文介绍了绑定到DataContext无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示任务的wpf窗口。 用户单击TreeView控件中的任务,然后其他控件(TextBox,ComboBox等)显示所选任务的各种属性。 我已经实现了如下:
$


1)TreeView被傀儡:

I have a wpf window that displays tasks.  The user clicks on a task in a TreeView control then other controls (TextBox, ComboBox, etc.) shows the various properties of the selected task.  I have implemented this as follows:

1) The TreeView is pupulated by:

ItemsSource="{Binding Source={StaticResource cvsTasks}}"

2)窗口的DataContext在代码隐藏中设置如下

2) The DataContext for the window is set in code-behind as follows

Public Class Tasks
   Private tsk As Task
   ....
   Private Sub LoadMe(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      DataContext = tsk
	....
   End Sub	

   Private Sub SelectTask(sender As Object, e As RoutedPropertyChangedEventArgs(Of Object)) Handles treTasks.SelectedItemChanged
      tsk = DirectCast(e.NewValue, Task)
   End Sub
....
End Class

3)每个任务属性使用如下所示的描述:

3) Each of the task properties using binding as shown below for the Description:

<TextBox
	x:Name="txtDescription"
	AcceptsReturn="True"
	Text="{Binding Path=Description}">
</TextBox>


这是不起作用的。 当用户选择TreeView项时,没有绑定控件显示相应的值。



我做错了什么?

and that is what is not working.  None of the controls with binding show corresponding value when a TreeView item is selected by the user.

What an I doing wrong?

推荐答案

b
不是明确哪个属性受到约束。查看此MVVM演示,了解如何显示所选节点。我使用来自nuget的  System.Windows.Interactivity.WPF。

Hi,
it is not clear which property is bound. Look this MVVM demo to unterstand how to show selected node. I use the System.Windows.Interactivity.WPF from nuget.

XAML:

<Window x:Class="Window09"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        Title="Window09" Height="450" Width="800">
  <Window.DataContext>
    <local:Window09VM/>
  </Window.DataContext>
    <Grid>
    <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TreeView ItemsSource="{Binding View}">
      <i:Interaction.Behaviors>
        <local:Window09Behavior/>
      </i:Interaction.Behaviors>
      <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Window09Data}"
                                  ItemsSource="{Binding SubItems}">
          <Label Content="{Binding Info}"/>
        </HierarchicalDataTemplate>
      </TreeView.Resources>
    </TreeView>
    <Label Grid.Row="1" Content="{Binding SelectedNode.Info}"/>
  </Grid>
</Window>

代码:

Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.Windows.Interactivity

Public Class Window09VM
  Implements INotifyPropertyChanged

  Public Sub New()
    For i = 1 To 10
      Dim d As New Window09Data With {.Info =


" Node {i}" ;}
for k = 1 to 5
d.SubItems.Add(New Window09Data With {.Info =
"Node {i}"} For k = 1 To 5 d.SubItems.Add(New Window09Data With {.Info =


" Subnode {i} {k}" ;})
下一个
col.Add(d)
下一个
cvs.Source = col
结束子

私人col as New ObservableCollection(Of Window09Data)
Private cvs As New CollectionViewSource

Public ReadOnly Property View As ICollectionView
Get
return cvs.View
End Get
结束财产

私人_selecte dNode As Window09Data
Public Property SelectedNode As Window09Data
Get
Return Me._selectedNode
End Get
Set(value As Window09Data)
Me._selectedNode = value
OnPropChanged()
结束集
结束属性

公共事件PropertyChanged As PropertyChangedEventHandler实现INotifyPropertyChanged.PropertyChanged
Private Sub OnPropChanged(< CallerMemberName>可选propName As String ="")
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(propName))
End Sub
End Class

Public Class Window09Data
Public Property Info As String
公共属性SubItems As New ObservableCollection(Of Window09Data)
End Class

Public Class Window09Behavior
Inherits Behavior(Of TreeView)
受保护的覆盖Sub OnAttached()
AddHandler AssociatedObject.SelectedItemChanged,AddressOf AssociatedObject_SelectedItemChanged

End Sub
受保护的覆盖Sub OnDetaching()
RemoveHandler AssociatedObject.SelectedItemChanged,AddressOf AssociatedObject_SelectedItemChanged

End Sub
Private Sub AssociatedObject_SelectedItemChanged(sender As Object,e As RoutedPropertyChangedEventArgs(Of Object))
CType(AssociatedObject.DataContext,Window09VM).SelectedNode =(CType(e。 NewValue,Window09Data))
End Sub
End Clas s
"Subnode {i} {k}"}) Next col.Add(d) Next cvs.Source = col End Sub Private col As New ObservableCollection(Of Window09Data) Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View End Get End Property Private _selectedNode As Window09Data Public Property SelectedNode As Window09Data Get Return Me._selectedNode End Get Set(value As Window09Data) Me._selectedNode = value OnPropChanged() End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Private Sub OnPropChanged(<CallerMemberName> Optional propName As String = "") RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName)) End Sub End Class Public Class Window09Data Public Property Info As String Public Property SubItems As New ObservableCollection(Of Window09Data) End Class Public Class Window09Behavior Inherits Behavior(Of TreeView) Protected Overrides Sub OnAttached() AddHandler AssociatedObject.SelectedItemChanged, AddressOf AssociatedObject_SelectedItemChanged End Sub Protected Overrides Sub OnDetaching() RemoveHandler AssociatedObject.SelectedItemChanged, AddressOf AssociatedObject_SelectedItemChanged End Sub Private Sub AssociatedObject_SelectedItemChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Object)) CType(AssociatedObject.DataContext, Window09VM).SelectedNode = (CType(e.NewValue, Window09Data)) End Sub End Class


这篇关于绑定到DataContext无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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