Reputation: 67
I have a listview in which's groupstyle, i have defined an expander(code below), i programmatically add many items to the listview which get added to the appropriate expander and if the expander does not exist fo the new item, it gets created dynamically.
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" >
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
So what needs to be done is that when a new item is added, the focus should divert to that item and the expander should be expanded while collapsing all else...
Upvotes: 2
Views: 5747
Reputation: 376
I wanted to expand on the previous answer -- I am using this in an MVVM scenario, and was getting the same error as Billy mentioned above
" A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."
I ended up modifying the converter and XAML as so (the DataContext.CurrentItem is a placeholder for whatever is needed to reference the current item in the View Model):
public class MultiBindingItemsEqualConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool valuesEquals = values[0].Equals(values[1]);
return valuesEquals;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
<Expander ...>
<Expander.IsExpanded>
<MultiBinding
Mode="OneWay"
Converter="{StaticResource MultiBindingItemsEqualConv}">
<Binding
RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
Path="SelectedItem"></Binding>
<Binding
RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
Path="DataContext.CurrentItem"></Binding>
</MultiBinding>
</Expander.IsExpanded>
</Expander>
Upvotes: 7
Reputation: 6961
Use a binding to see if the lists SelectedItem is part of the Group that we are bound to.
<Expander IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}, Path=SelectedItem, Converter={StaticResource yourConverter}, ConverterParameter={Binding}}" >
You will Bind IsExpanded to the lists SelectedItem, with a converter parameter that is bound to the viewmodel and have the converter simply check to see if the arguments match.
The converter simply returns true or false
public class yourConvertor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((GroupItem)parameter).Contains(value);
}
}
Upvotes: 2