如果我理解正確的問題,要選擇(或以其他方式突出顯示)的其只要在底部列表中選擇了該項目,就會將它們放在頂部列表中。您可以使用數據綁定做到這一點,例如:
<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