如果我理解正确的问题,要选择(或以其他方式突出显示)的其只要在底部列表中选择了该项目,就会将它们放在顶部列表中。您可以使用数据绑定做到这一点,例如:
<ListView
Grid.Row="0"
ItemsSource="{Binding MenuCategories}"
Margin="8"
SelectedIndex="{Binding SelectedIndex, ElementName=verticalList, Mode=OneWay}"
>
这里的顶级目录的SelectedIndex
属性反映了底部列表中。
在上下文中,这样的事情:
<Page.Resources>
<Style x:Key="CustomButtonStyle" TargetType="Button" />
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListView
Grid.Row="0"
ItemsSource="{Binding MenuCategories}"
Margin="8"
SelectedIndex="{Binding SelectedIndex, ElementName=verticalList, Mode=OneWay}"
>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:CategoryModel">
<Button
HorizontalAlignment="Center"
VerticalAlignment="Top"
Tag="{Binding CategoryId}"
Content="{Binding CategoryName}"
Style="{StaticResource CustomButtonStyle}"
FontFamily="Segoe UI"
FontWeight="SemiBold"
FontSize="18"
Margin="0"
Padding="20"
BorderBrush="Red"
BorderThickness="0"
Opacity="0.5"
Foreground="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ListView
x:Name="verticalList"
Grid.Row="1"
ItemsSource="{Binding MenuCategories}"
Margin="8"
IsItemClickEnabled="True"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:CategoryModel">
<Button
HorizontalAlignment="Center"
VerticalAlignment="Top"
Tag="{Binding CategoryId}"
Content="{Binding CategoryName}"
Style="{StaticResource CustomButtonStyle}"
FontFamily="Segoe UI"
FontWeight="SemiBold"
FontSize="18"
Margin="0"
Padding="20"
BorderBrush="Red"
BorderThickness="0"
Opacity="0.5"
Foreground="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
警告:当您点击一个按钮,该ListView
没有看到,点击;如果你想要点击选择一个项目,请在代码隐藏中执行。
还要注意第二个(垂直)列表上的IsItemClickEnabled
属性。
编辑:如果我理解你正确,你想在上部水平列表中的选择跟踪滚动在较低的垂直而不是选择。在这种情况下,你需要获得的内置ScrollViewer
和做这样的事情搁置:
public MainPage()
{
InitializeComponent();
DataContext = this;
Loaded += (sender, args) =>
{
ScrollViewer scrollViewer = FindVisualChild<ScrollViewer>(verticalList);
if (scrollViewer != null)
{
scrollViewer.ViewChanged += (o, eventArgs) =>
{
int length = MenuCategories.Length;
double offset = scrollViewer.VerticalOffset;
double height = scrollViewer.ExtentHeight;
int index = (int)(length * offset/height);
horizontalList.SelectedIndex = index;
};
}
};
}
private static T FindVisualChild<T>(DependencyObject parent)
where T : DependencyObject
{
if (parent != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
T candidate = child as T;
if (candidate != null)
{
return candidate;
}
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return default(T);
}
你可能需要在这里的计算实验;这在我的方面有点实验。
你能分享你的XAML的? – 2016-01-24 22:30:01
我真的不明白,你需要知道,对不起。你想知道,底部列表框中的哪个元素被选中,那么你可以突出显示顶部列表框中的相同元素? –
是@einRobby。我想突出显示顶部,取决于底部的菜单元素 – Vantage