问题
I currently have a WPF Treeview with a structure of:
--> Group 1 (Hierarchical Group)
---> Group 2 (Hierarchical Group)
---> Item 1
---> Item 2
---> Item 3
I currently have a SelectedItemChanged event handler hooked up to this treeview like so
private void TreeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue is Item)
{
Item item = e.NewValue as Item;
if (Item != SelectedItem)
{
//keep SelectedItem in sync with Treeview.SelectedItem
SelectedItem = e.NewValue as Item;
}
}
else
{
//if the user tries to select an object that isn't an Item (i.e. a group) reselect the first Item in that group
//This will then cause stack overflow in methods I've tried so far
}
}
So my question is, how can I keep the SelectedItem of the treeview in sync with my SelectedItem property in my code behind and also how can I reselect an item if the user selects a group?
EDIT:
<TreeView Grid.Row="1" Grid.RowSpan="2"
ItemContainerStyle="{StaticResource StandardListStyle}"
ItemTemplate="{StaticResource TreeViewItemTemplate}"
BorderThickness="1,0,1,1"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Teams}"
SelectedItemChanged="TreeViewControl_SelectedItemChanged"
Loaded="OnTreeViewLoaded"
x:Name="TreeViewControl">
where:
Teams = List<HierachicalGroup>;
and where:
public class HierachicalGroup
{
public virtual string Name { get; set; }
public virtual HierachicalGroup[] Children { get; set; }
public virtual HierachicalGroup Parent { get; set; }
}
and item is:
public class Item: HierachicalGroup
{
public Domain Domain { get; set; }
public override string Name
{
get
{
return Domain.DomainName;
}
}
}
回答1:
In TreeView.SelectedItemChanged event, we get e.NewValue which is type of TreeViewItem, so I think you can use following code to select an item when it's group is selected.
var item = (e.NewValue as TreeViewItem);
if (item.Items.Count > 0)
{
(item.Items[0] as TreeViewItem).IsSelected = true;
}
回答2:
You haven't really shown enough for a solid answer, but you must have set a collection of some type as the TreeView.ItemsSource
property. Therefore all of your items in that collection must be of that type, maybe a base type. You just need to make your SelectedItem
property the same type as those items in the collection and then you could do this:
SelectedItem = e.NewValue as TypeOfItemsInCollection;
Or, you could just add a SelectedGroupItem
property of type Group
(or whatever you need) and do this:
if (e.NewValue is Item)
{
SelectedItem = e.NewValue as Item;
}
else if (e.NewValue is Group)
{
SelectedGroupItem = e.NewValue as Group;
}
回答3:
Ok so this was a lot more complicated than I initially thought.
I used this link for help: WPT TreeView ViewModel Pattern
<TreeView Grid.Row="1" Grid.RowSpan="2"
ItemTemplate="{StaticResource TreeViewItemTemplate}"
BorderThickness="1,0,1,1"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Teams}"
SelectedItemChanged="TreeViewControl_SelectedItemChanged"
Loaded="OnTreeViewLoaded"
x:Name="TreeViewControl">
<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>
Where:
public class HierachicalGroup: INotifyPropertyChanged
{
public virtual string Name { get; set; }
public virtual HierachicalGroup[] Children { get; set; }
public virtual HierachicalGroup Parent { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
private bool _isExpanded;
public bool IsExpanded
{
get { return _isExpanded; }
set
{
if (value != _isExpanded)
{
_isExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
// Expand all the way up to the root.
if (_isExpanded && Parent != null)
Parent.IsExpanded = true;
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion // INotifyPropertyChanged Members
}
and:
public class Item: HierachicalGroup, INotifyPropertyChanged
{
public Domain Domain { get; set; }
public override string Name
{
get
{
return Domain.DomainName;
}
set
{
//not doing it :)
}
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
and Finally:
private void TreeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue is Item)
{
Item item = e.NewValue as Item;
if (Item != SelectedItem)
{
//keep SelectedItem in sync with Treeview.SelectedItem
SelectedItem = e.NewValue as Item;
}
}
else
{
var item = e.NewValue as HierarchicalGroup;
item.IsExpanded = true;
if (item.Children.Count() > 0)
{
if (item.Children[0] is Item)
{
(item.Children[0] as Item).IsSelected = true;
}
}
}
}
来源:https://stackoverflow.com/questions/24756011/changing-wpf-treeview-selecteditem-in-selecteditemchanged-event