开发者

WPF Moving Element problem

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!


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜