WPF移动元素问题

时间:2010-05-24 19:46:43

标签: c# wpf button resize

我可能过度做了一个非常简单的问题,但这就是我现在所拥有的:

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

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

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

2 个答案:

答案 0 :(得分:0)

不确定为什么在调整大小后无法再点击这些项目。这可能有很多原因。显然你想在ListBox周围有比例边距。你通常会使用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>

答案 1 :(得分:0)

我写这个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>

使用此代码隐藏:

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

但我无法重现您的问题。检查我的代码是否与您的代码有什么不同并告诉我什么,所以我可以给你一些建议。谢谢