Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何知道在windows 10中的通用windows应用程序中哪个元素处于焦点_C# 4.0_Uwp_Windows 10 Universal - Fatal编程技术网

C# 4.0 如何知道在windows 10中的通用windows应用程序中哪个元素处于焦点

C# 4.0 如何知道在windows 10中的通用windows应用程序中哪个元素处于焦点,c#-4.0,uwp,windows-10-universal,C# 4.0,Uwp,Windows 10 Universal,我在Windows10的通用windows应用程序中有一个xaml页面。此页面包含两个列表框。两个列表框具有相同的项源,如 public class CategoryModel { public int CategoryId {get; set;} public string CategoryName {get; set;} public List<string> ImageURL {get; set;} } 公共类类别模型 { public int CategoryId{g

我在Windows10的通用windows应用程序中有一个xaml页面。此页面包含两个列表框。两个列表框具有相同的项源,如

public class CategoryModel
{
 public int CategoryId {get; set;}
 public string CategoryName {get; set;}
 public List<string> ImageURL {get; set;}
}
公共类类别模型
{
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
公共列表ImageURL{get;set;}
}
顶部列表框以水平方式在顶部创建菜单标题,底部列表框以垂直方式在菜单标题底部创建菜单数据

问题是,如何知道底部的哪个菜单数据元素处于焦点位置,以便在菜单标题中突出显示相同的元素

                    <ListView x:Name="lvMenuBar" Grid.Column="1" FlowDirection="LeftToRight" ItemsSource="{Binding MenuCategories}" Width="Auto">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Button Click="MenuBarClick" 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="lvMenuBar" Grid.Column="1" FlowDirection="LeftToRight" ItemsSource="{Binding MenuCategories}" Width="Auto">
                        <ListView.ItemTemplate>
                            <DataTemplate>                                    


以上是我的XAML

如果我正确理解了问题,那么您希望在顶部列表中选择(或突出显示)底部列表中的项目。您可以通过数据绑定来实现这一点,例如:

<ListView
    Grid.Row="0"
    ItemsSource="{Binding MenuCategories}"
    Margin="8"
    SelectedIndex="{Binding SelectedIndex, ElementName=verticalList, Mode=OneWay}"
    >
警告:当您单击其中一个按钮时,
列表视图
看不到该单击;如果您希望单击以选择项目,请在代码隐藏中执行

另请注意第二个(垂直)列表中的
IsItemClickEnabled
属性


编辑:如果我理解正确,您希望上水平列表中的选择跟踪下垂直滚动,而不是选择。在这种情况下,您需要掌握内置的
ScrollViewer
,并执行以下操作:

<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>
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);
}
public主页()
{
初始化组件();
DataContext=this;
已加载+=(发送方,参数)=>
{
ScrollViewer ScrollViewer=FindVisualChild(verticalList);
如果(scrollViewer!=null)
{
scrollViewer.ViewChanged+=(o,eventArgs)=>
{
int length=MenuCategories.length;
双偏移=scrollViewer.VerticalOffset;
双倍高度=scrollViewer.ExtentHeight;
int索引=(int)(长度*偏移/高度);
horizontalList.SelectedIndex=索引;
};
}
};
}
私有静态T FindVisualChild(DependencyObject父对象)
其中T:DependencyObject
{
如果(父项!=null)
{
for(int i=0;i

您可能需要在这里进行计算实验;这对我来说有点实验性。

这段代码解决了我的大部分问题

    private double _scrollExtentHeight;
    private ScrollViewer _scrollViewer; 

_scrollViewer = FindVisualChild<ScrollViewer>(lvMenuItems);
        if (_scrollViewer != null)
        {
            _scrollViewer.ManipulationMode = ManipulationModes.TranslateY;
            _scrollViewer.DirectManipulationCompleted += scrollViewerDirectManipulationCompleted;
            _scrollExtentHeight = _scrollViewer.ExtentHeight;
        }



    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);
    }

    private void scrollViewerDirectManipulationCompleted(object sender, object e)
    {
        _menuVM.StartDispatcher();
        if (_scrollViewer != null)
        {
            int length = _menuVM.Categories.Count;
            double offset = _scrollViewer.VerticalOffset;
    //Horizontal scroll viewer
            List<Button> menuItems = GetAllMenuItemControl(lvMenuBar); 
            int currIndex = 0, index = 0;
    //Categories height ratio contains the height ratio of each element for total height
            for (; index < _menuVM.CategoriesHeightRatio.Count; index++)
            {
                if ((_menuVM.CategoriesHeightRatio[index - 1 > 0 ? index - 1 : 0] * _scrollExtentHeight) < offset && (_menuVM.CategoriesHeightRatio[index] * _scrollExtentHeight) >= offset)
                {
                    currIndex = index;
                }
                else
                {
                    menuItems[index].BorderThickness = new Thickness(0, 0, 0, 0);
                    menuItems[index].Opacity = 0.5;
                }
            }
            menuItems[currIndex].BorderThickness = new Thickness(0, 0, 0, 2);
            menuItems[currIndex].Opacity = 1;
            var transform = lvMenuBar.TransformToVisual(menuItems[currIndex]);
            Point absolutePoint = transform.TransformPoint(new Point(0, 0));
            svMenuBar.ChangeView(Math.Abs(absolutePoint.X), null, null, false);
        }
    }

    private List<Button> GetAllMenuItemControl(DependencyObject parent)
    {
        var _List = new List<Button>();
        for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++)
        {
            var _Child = VisualTreeHelper.GetChild(parent, index);
            if (_Child is Button)
                _List.Add(_Child as Button);
            _List.AddRange(GetAllMenuItemControl(_Child));
        }
        return _List;
    }
private-double\u-scroll扩展权限;
私有ScrollViewer\u ScrollViewer;
_scrollViewer=FindVisualChild(lvMenuItems);
如果(_scrollViewer!=null)
{
_scrollViewer.操纵模式=操纵模式.TranslateY;
_scrollViewer.DirectOperationCompleted+=ScrollViewerDirectOperationCompleted;
_scrollExtentHeight=\u scrollViewer.ExtentHeight;
}
私有静态T FindVisualChild(DependencyObject父对象)
其中T:DependencyObject
{
如果(父项!=null)
{
for(int i=0;i0?index-1:0]*\u scrollExtentHeight)=offset)
{
currIndex=指数;
}
其他的
{
menuItems[index].BorderThickness=新厚度(0,0,0,0);
menuItems[索引]。不透明度=0.5;
}
}
menuItems[currIndex]。边界厚度=新厚度(0,0,0,2);