WPF ListBox项目滚动/单击时设置新位置并显示下拉菜单无效。

huangapple go评论84阅读模式
英文:

WPF ListBox item scroll/setting a new position when clicking and showing a drop down menu does not work

问题

I have a ListBox control where I have some custom items with custom drop-down menus.

现在我有一个ListBox控件,其中包含一些自定义项目和自定义下拉菜单。

Now, I have a new problem and I am attaching a video clip. so, I would like my bottom items to behave as the one above on clicking the item.

现在,我遇到了一个新问题,并附上了一个视频剪辑。所以,我希望在点击底部项目时,它们的行为与上面的项目相同。

So, when I click on the bottom item I want the upper item to go up in order to show the drop-down menu of the last on item.

因此,当我点击底部项目时,我希望上面的项目上移,以显示最后一个项目的下拉菜单。

You can see that when I click the above items they change the size and move the bottom item in order to show the drop-down menu.

您可以看到,当我点击上面的项目时,它们会更改大小并移动底部项目以显示下拉菜单。

The same principle is working on other custom listbox but here not and perhas because of thise custom items(which are only present here).

相同的原理在其他自定义ListBox上起作用,但在这里却没有,可能是因为这些自定义项(仅在这里存在)。

Please take a look at the video clip I am sending.

请查看我发送的视频剪辑。

You can find the data template code which defines the item.

您可以找到定义项目的数据模板代码。

UPDATE - WORKING CONTROL LISTBOX:

更新 - 正常工作的控制ListBox:

英文:

I have a ListBox control where I have some custom items with custom drop-down menus(my question from a couple of days ago - https://stackoverflow.com/questions/76367378/how-to-show-a-listbox-item-outside-the-listbox-control-and-over-all-other-contro).

Now, I have a new problem and I am attaching a video clip. so, I would like my bottom items to behave as the one above on clicking the item. So, when I click on the bottom item I want the upper item to go up in order to show the drop-down menu of the last on item. You can see that when I click the above items they change the size and move the bottom item in order to show the drop-down menu. The same principle is working on other custom listbox but here not and perhas because of thise custom items(which are only present here). Please take a look at the video clip I am sending. You can find the data template code which defines the item.WPF ListBox项目滚动/单击时设置新位置并显示下拉菜单无效。

<DataTemplate x:Key="MYListBoxItemDataTemplate" DataType="{x:Type models:MyType}">
        <StackPanel x:Name="itemsactions" HorizontalAlignment="Center" Height="135">
            <ToggleButton x:Name="choose" Height="52" Margin="0,27,0,0"
                          IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                          Command="{Binding DataContext.CurrentViewModel.ClickPalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                          CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                          Style="{DynamicResource CircledTransparentToggleButtonStyle}">
                <Image x:Name="image" VerticalAlignment="Center" Margin="0,0,0,0" Height="52" Stretch="Uniform">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding ImageSource.Regular}"/>
                    </Image.Source>
                </Image>
            </ToggleButton>
            <StackPanel HorizontalAlignment="Center">
                <Label Margin="0,15,0,0" Width="152" HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
                       FontSize="14" FontWeight="SemiBold" 
                       Content="{Binding Weight}" 
                       Style="{DynamicResource LeftLoginTextBoxBlockLabelStyle}"/>
                <Label Margin="0,2,0,0" Width="152" HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
                       FontSize="14" 
                       Content="{Binding Size}" 
                       Style="{DynamicResource LeftLoginTextBoxBlockLabelStyle}"/>
            </StackPanel>
            <StackPanel Margin="0,34,0,0" x:Name="subactions" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
                <ToggleButton x:Name="delete" IsTabStop="False" Width="70"
                              IsChecked="{Binding DataContext.CurrentViewModel.IsDeleteConfirmed, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                              Style="{StaticResource DeleteConfirmToggleButtonStyle}"
                              Command="{Binding DataContext.CurrentViewModel.ConfirmDeletePalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                              CommandParameter=""/>
                <Button x:Name="edit" IsTabStop="False" HorizontalAlignment="Left" Height="40" Style="{DynamicResource EditButtonStyle}" Content="Edit"
                        Command="{Binding Path=DataContext.CurrentViewModel.EditPalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
            </StackPanel>
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsChecked, ElementName=choose}" Value="True">
                <Setter Property="Visibility" TargetName="subactions" Value="Visible"/>
                <Setter Property="Height" TargetName="itemsactions" Value="200"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsMouseOver, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.MouseOver}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsPressed, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Pressed}"/>
                <Setter Property="Visibility" TargetName="subactions" Value="Visible"/>
                <Setter Property="Height" TargetName="itemsactions" Value="200"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsChecked, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Choosen}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsEnabled, ElementName=choose}" Value="False">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Disabled}"/>
                <Setter Property="Height" TargetName="itemsactions" Value="135"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

UPDATE - WORKING CONTROL LISTBOX:WPF ListBox项目滚动/单击时设置新位置并显示下拉菜单无效。

答案1

得分: 1

下拉菜单存在于切换按钮所在的父级内部。您正在使用的网格布局可能会被调整大小以适应,然后,一切都会被重新绘制以适应位于父级内部的新内容。

我不太写XAML(我只使用事件并编写C#代码...),但我想您可能需要以某种方式停止网格的调整大小,或者将您可见的堆栈面板标记为不是按钮父级的子级。

我不知道如何做到这两点。

英文:

The dropdown exists "inside" the parent of the toggle button thing. The grid layout you are using probably gets resized to fit, and then voila! Everything is redrawn 'correctly' to fit the new content that is 'inside the parent'.

I don't write much xaml (I just use the events and write c# code...), but I suppose you somehow need to stop the resize of your grid, or mark the stack panel you make visible NOT a child of the button's parent.

I don't know how to do either.

答案2

得分: 1

工作控件在展开面板中有三个元素,而非工作容器中只有两个元素。这使得 ListBoxItem 的高度更大。看起来这足以触发 ListBox 滚动将项目滚动到视图中。

您可以尝试两件事情:
1)向展开容器添加第三个元素(或通常增加展开项的高度)
2)处理 ListBox.SelectionChangedToggleButton.Clicked 事件,获取处理程序中选定的项目容器并调用 FrameworkElement.BringIntoView()。这可能足以自动滚动整个项目到视图中。

<ListBox SelectionChanged="OnSelectionChanged" />
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
  var listBox = sender as ListBox;
  var selectedItemContainer = listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem) as FrameworkElement;
  selectedItemContainer?.BringIntoView();
}
英文:

The working control has three elements in the expanded panel opposed to the two elements in the non-working container. This gives the ListBoxItem a greater height. It seems that this is enough to trigger the ListBox to scroll the item into view.

You can try two things:

  1. add a third element to the expanded container (or generally increase the expanded item height)
  2. Handle the ListBox.SelectionChanged or ToggleButton.Clicked event, get the selected item container in the handler and call FrameworkElement.BringIntoView(). This could be enough to scroll the complete item into view automatically.
<ListBox SelectionChanged="OnSelectionChanged" />
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
  var listBox = sender as ListBox;
  var selectedItemContainer = listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem) as FrameworkElement;
  selectedItemContainer?.BringIntoView();
}

huangapple
  • 本文由 发表于 2023年6月12日 00:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定