WPF移动元素问题

发布于 2024-09-02 13:48:57 字数 338 浏览 2 评论 0原文

我可能过度解决了一个非常简单的问题,但这就是我现在所遇到的:

我有几个按钮和一个项目列表框,用户可以在其中选择并与之交互。我的应用程序还根据应用程序宽度/高度移动这些元素,如下所示:

listBox1.Margin = new Thickness(this.ActualWidth * 0.84, this.ActualHeight * 0.3, 0, 0);

我能够在窗口模式下选择列表框中的项目并适当地单击按钮,但是当我开始将应用程序拉伸得更大时,我尝试单击这些项目,但我不能这样做..这是因为我也还需要更新他们的命中检测矩形吗?或者也许我错误地移动了物品?我不知所措……此时任何信息都会非常有帮助……谢谢!

I am probably over-doing a very simple problem, but this is what I have a the moment:

I have several buttons and a listbox of items in which the user can select and interact with. My application also moves those elements in accordance to the application width/height, such as follows:

listBox1.Margin = new Thickness(this.ActualWidth * 0.84, this.ActualHeight * 0.3, 0, 0);

I am able to select the items within the listbox and click on buttons appropriately while in windowed mode, but as I begin to stretch the application larger, I try to click on the items, and I cannot do so.. is this because I also need to update their hit-detection rectangles as well? Or perhaps am I moving the items incorrectly? I am at a loss.. any information would be very helpful at this point...thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-09-09 13:48:57

不知道为什么调整大小后无法再单击这些项目。这可能有很多原因。显然您希望列表框周围有比例的边距。您通常会使用网格来做到这一点:

<Grid>
  <Grid.ColumnDefinitions>
     <ColumnDefinition Width="0.83*"/>
     <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
     <RowDefinition Height="0.3*"/>
     <RowDefinition/>
  </Grid.RowDefinitions>
  <ListBox x:Name="listBox1" Grid.Column="1" Grid.Row="1"/>
</Grid>

Not sure why you can't click the items anymore after resizing. That can have a lot of reasons. Obviously you want to have proportional margins around the ListBox. You would normally do that with a Grid:

<Grid>
  <Grid.ColumnDefinitions>
     <ColumnDefinition Width="0.83*"/>
     <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
     <RowDefinition Height="0.3*"/>
     <RowDefinition/>
  </Grid.RowDefinitions>
  <ListBox x:Name="listBox1" Grid.Column="1" Grid.Row="1"/>
</Grid>
离旧人 2024-09-09 13:48:57

<Window x:Class="StackOverflow_MovingProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox Name="listbox1" ItemsSource="{Binding Path=list}" />
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="button1" />
            <Button Content="button2" />
            <Button Content="button3" />
        </StackPanel>
    </Grid>
</Window>

用这个代码隐藏

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);

            list = new ObservableCollection<string>();
            list.Add("item1");
            list.Add("item2");
            list.Add("item3");
        }

        public ObservableCollection<string> list { get; set; }

        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            listbox1.Margin = new Thickness(this.ActualWidth * 0.84,
                                            this.ActualHeight * 0.3, 0, 0);
        }
    }

编写了这个 XAML:但我无法重现您的问题。检查我的代码是否与您的代码有不同之处并告诉我什么,以便我可以给您一些建议。谢谢

I write this XAML:

<Window x:Class="StackOverflow_MovingProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox Name="listbox1" ItemsSource="{Binding Path=list}" />
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="button1" />
            <Button Content="button2" />
            <Button Content="button3" />
        </StackPanel>
    </Grid>
</Window>

with this code-behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);

            list = new ObservableCollection<string>();
            list.Add("item1");
            list.Add("item2");
            list.Add("item3");
        }

        public ObservableCollection<string> list { get; set; }

        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            listbox1.Margin = new Thickness(this.ActualWidth * 0.84,
                                            this.ActualHeight * 0.3, 0, 0);
        }
    }

but I'm not able to reproduce your problem. Check my code if there is something different with your code and tell me what, so I can give you some advices. Thank you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文