我試圖讓所選TreeView節點內部視圖模型獲取TreeView控件的SelectedItem內部視圖模型
裏面的XAML
<Grid>
<TreeView ItemsSource="{Binding TreeViewModel.TreeData}">
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding GroupTables}" DataType="{x:Type tbl:StaticTablesGroup}">
<Label Content="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type tbl:GroupTable}">
<Label Content="{Binding Name}"/>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
內VieModel
public class TreeTablesViewModel : ViewModelBase
{
private TablesXML _tables;
public TreeTablesViewModel()
{
_tables = Deserialize.GuymasterXml();
}
public List<StaticTablesGroup> TreeData
{
get
{
return _tables.StaticTablesGroups;
}
set
{
_tables.StaticTablesGroups = value;
OnPropertyChanged("TreeData");
}
}
}
,最後的數據類
[XmlRoot("Tables")]
public class TablesXML
{
public TablesXML()
{
StaticTablesGroups = new List<StaticTablesGroup>();
}
[XmlArray("StaticGroups")]
[XmlArrayItem("StaticGroup", typeof(StaticTablesGroup))]
public List<StaticTablesGroup> StaticTablesGroups { get; set; }
}
public class StaticTablesGroup
{
public StaticTablesGroup()
{
GroupTables = new List<GroupTable>();
}
[XmlAttribute("Name")]
public string Name { get; set; }
//[XmlArray("StaticGroup")]
[XmlElement("Table", typeof(GroupTable))]
public List<GroupTable> GroupTables { get; set; }
}
public class GroupTable
{
[XmlElement("TableName")]
public string Name { get; set; }
[XmlElement("TableTag")]
public string Tag { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected != value)
{
_isSelected = value;
}
}
}
}
選擇Treeview節點後,在GroupTable或StaticTablesGroup中引發IsSelected。 如何獲取TreeTablesViewModel中的選定節點?
謝謝
後面添加代碼,謝謝您的答覆。我將Selecteditem屬性添加到TreeTablesViewModel:private object _selectedItem; 公共對象SelectedItem { 得到 { return _selectedItem; } set { _selectedItem = value; OnPropertyChanged(「SelectedItem」); } }但是,viewModel內部的SelectedItem從不執行。我究竟做錯了什麼。 – Vadim
在XAML: –
Vadim
做你試圖將一個破發點的SelectedItemChanged處理程序中的行爲,看是否該事件得到提高? – Bruno