问题描述
|
我在Winforms应用中有一个WPF
ElementHost
。用户控件中包含一些文本和一个“ 1”,其中应显示该应用程序提供的可用命令树。
我是WPF的新手(这是一项学习练习),因此绑定数据时遇到一些问题。
我创建了一个CommandTreeviewmodel
类来充当我的视图模型。它具有FirstGeneration
属性,即IEnumerable(of Commandviewmodel)
。 Commandviewmodel
类又具有描述describing6ѭ的一些简单属性,包括Children
属性(还是IEnumerable(of Commandviewmodel)
)。
我已经在我的winforms应用设置的WPF用户控件中添加了ѭ9。
我不知道该怎么做,就是将已经交给viewmodel属性的数据拿来绑定到TreeView上。 (还有什么方法可以强力键入XAML绑定的viewmodel类Ala MVC?)
我在下面包括了我认为是相关代码,以备需要时使用。
用户控制
Public Class WPFCommandTree
Implements INotifyPropertyChanged
Public Property viewmodel As CommandTreeviewmodel
Get
Return DirectCast(GetValue(viewmodelProperty),CommandTreeviewmodel)
End Get
Set(ByVal value As CommandTreeviewmodel)
If Not value.Equals(DirectCast(GetValue(viewmodelProperty),CommandTreeviewmodel)) Then
SetValue(viewmodelProperty,value)
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(\"viewmodelProperty\"))
End If
End Set
End Property
Public Shared ReadOnly viewmodelProperty As DependencyProperty = _
DependencyProperty.Register(\"viewmodel\",GetType(CommandTreeviewmodel),GetType(Window),New FrameworkPropertyMetadata(nothing))
Public Event PropertyChanged(ByVal sender As Object,ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
XAML
<UserControl x:Class=\"WPFCommandTree\"
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"
xmlns:d=\"http://schemas.microsoft.com/expression/blend/2008\"
mc:Ignorable=\"d\"
d:DesignHeight=\"300\" d:DesignWidth=\"300\">
<Grid>
<Grid.RowDeFinitions>
<RowDeFinition Height=\"60\" />
<RowDeFinition Height=\"*\" />
</Grid.RowDeFinitions>
<TextBlock FontSize=\"30\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\">Test</TextBlock>
<TreeView ItemsSource=\"{Binding FirstGeneration}\"
VerticalAlignment=\"Stretch\"
HorizontalAlignment=\"Stretch\"
Grid.Row=\"1\"
DataContext=\"{Binding}\">
<TreeView.ItemContainerStyle>
<Style targettype=\"{x:Type TreeViewItem}\">
<Setter Property=\"IsExpanded\"
Value=\"{Binding IsExpanded,Mode=TwoWay}\" />
<Setter Property=\"IsSelected\"
Value=\"{Binding IsSelected,Mode=TwoWay}\" />
<Setter Property=\"FontWeight\"
Value=\"normal\" />
<Style.Triggers>
<Trigger Property=\"IsSelected\" Value=\"True\">
<Setter Property=\"FontWeight\" Value=\"Bold\" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource=\"{Binding Children}\">
<TextBlock Text=\"{Binding Name}\" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</UserControl>
查看模型
Public Class CommandTreeviewmodel
Public Property RootCommand As Commandviewmodel
Public Property FirstGeneration As ReadOnlyCollection(Of Commandviewmodel)
Public Sub New(ByVal RootCommand As Command)
_RootCommand = New Commandviewmodel(RootCommand)
_FirstGeneration = New ReadOnlyCollection(Of Commandviewmodel)(New Commandviewmodel() {_RootCommand})
End Sub
Public Sub New(ByVal RootCommand As Commandviewmodel)
Me.RootCommand = RootCommand
Me.FirstGeneration = New ReadOnlyCollection(Of Commandviewmodel)(New Commandviewmodel() {_RootCommand})
End Sub
Public Sub New(ByVal RootCommands As IEnumerable(Of Commandviewmodel))
Me.RootCommand = RootCommands.First
Me.FirstGeneration = New ReadOnlyCollection(Of Commandviewmodel)(RootCommands.ToList)
End Sub
End Class
Public Class Commandviewmodel
Implements INotifyPropertyChanged
Public Property Command As Command
Public Property Children As ReadOnlyCollection(Of Commandviewmodel)
Public Property Parent As Commandviewmodel
Public Property Name As String
Private Property _IsSelected As Boolean
Public Property IsSelected() As Boolean
Get
Return _isSelected
End Get
Set(ByVal value As Boolean)
If value <> _isSelected Then
_isSelected = value
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(\"IsSelected\"))
End If
End Set
End Property
Private Property _IsExpanded As Boolean
Public Property IsExpanded() As Boolean
Get
Return _IsExpanded
End Get
Set(ByVal value As Boolean)
If value <> IsExpanded Then
_IsExpanded = value
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(\"IsExpanded\"))
If _IsExpanded And _Parent IsNot nothing Then
_Parent.IsExpanded = True
End If
End If
End Set
End Property
Public Sub New(ByVal Command As Command)
Me.New(Command,nothing)
End Sub
Private Sub New(ByVal Command As Command,ByVal Parent As Commandviewmodel)
_Command = Command
_Parent = Parent
If Command.Children IsNot nothing AndAlso Command.Children.Count > 0 Then
_Children = New ReadOnlyCollection(Of Commandviewmodel)(
Command.Children.Select(Function(x) New Commandviewmodel(x,Me)
).ToList)
End If
End Sub
Public Event PropertyChanged(ByVal sender As Object,ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
如您所见,我不清楚我需要做什么,并且一直在尝试从各种教程中破解一些代码。大多数仅是WPF。
运行上面的命令可以正确加载cotrol(我可以在表单上看到\“ Test \”文本),但是ѭ1仍为空白。没有引发任何错误。我假设这是因为我没有正确绑定数据。
最后,我也不清楚我的哪些属性需要成为DP?用户控件,viewmodel,子模型?我只是不清楚绑定的工作方式。
解决方法
有很多代码需要解析,而且我已经有一段时间没有在VB中进行编码了,但是我至少看到了几件事。
我建议您更简单地开始。如果您真的才刚刚入门,请首先尝试获取易于绑定的内容,而不要使用具有多个级别命令的树形视图。尝试使用平面数据网格进行绑定,然后再使用分层数据模板,添加样式(使用触发器!)等。您一次引入了太多的概念恕我直言。
我认为您根本不需要在这里使用依赖项属性,除非我遗漏了一些东西。依赖属性主要用于要将用户控件拖放到另一个控件/窗口中并能够通过XAML / Databinding控制该属性的情况。
看来您正在尝试通过依赖项属性设置viewModel。相反,只需在代码背后的构造函数中设置DataContext属性-类似于Me.DataContext = New CommandTreeViewModel(请记住我的VB生锈:))。实际上,这可能是绑定不起作用的主要问题,因为未设置View \的DataContext。
最后,VS中的调试输出窗口应包含任何数据绑定错误,这对于找出绑定失败的位置有很大帮助。