我有一个带有自定义堆栈面板的列表框(只是一个类扩展了一个堆栈面板,但是我希望在这里做一些动画)作为它的项目。现在,当选择发生变化时,我想在最后一个被选中的项目和当前选定的项目之间做一些很好的动画。
现在,我的问题是如何在项目列表中获得选定的项?
我就是这样定义我的项目的
<ItemsPanelTemplate>
<l:CustomStackPanel SelectedItem="{Binding SelectedItem,ElementName=listbox}" IsItemsHost="True" Orientation="Vertical"/>
</ItemsPanelTemplate>我在自定义堆栈面板中创建了一个名为SelectedItem的依赖项属性
public UIElement SelectedItem
{
get { return (UIElement)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedItem. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(UIElement), typeof(CustomStackPanel), new PropertyMetadata(null,selectionChanged));我认为我可以简单地将列表框中的selectedItem绑定到堆栈面板中的选择项。但这种方法根本行不通。
另一个想法是重写堆栈面板上的previewmousedown,并从堆栈面板的Children中找到相应的项。但我还是不知道如何找到这个项目。
发布于 2014-06-26 09:35:44
在绑定中使用RelativeSource
<ItemsPanelTemplate>
<l:CustomStackPanel SelectedItem="{Binding SelectedItem,RelativeSource={RelativeSource FindAncestor, AncestorType=x:Type ListBox}}" IsItemsHost="True" Orientation="Vertical"/>
</ItemsPanelTemplate>https://stackoverflow.com/questions/24426668
复制相似问题