Setting content of a ScatterViewItem depending on the size

落爺英雄遲暮 提交于 2019-12-14 02:53:36

问题


I registered a SizeChangedHandler on my ScatterViewItem:

private void MethodBoxScatterSizeChanged(object sender, SizeChangedEventArgs args)
        {
            if (args.NewSize.Width < 150 && args.NewSize.Height < 150)
            {
                ScatterViewItem svi = sender as ScatterViewItem;

                TextBox txt = new TextBox();
                txt.Text = "Test";
                txt.Tag = svi.Content;
                svi.Content = txt;
                args.Handled = true;
            }
            else if (args.PreviousSize.Width < 150 && args.PreviousSize.Height < 150 && args.NewSize.Height > 150 && args.NewSize.Width > 150)
            {
                ScatterViewItem svi = sender as ScatterViewItem;
                FrameworkElement old = (svi.Content as FrameworkElement).Tag as FrameworkElement;
                svi.Content = old;
                args.Handled = true;
            }
        }

As you can see, I want to set Test as the content of the ScatterViewItem if it is small, and the original content if it is bigger again. But once I decreased it, it always stays the Test way. What am I doing wrong?


回答1:


in the if statement, you aren't checking whether you've already switched to "Test". so if you get two events saying it has resized to be smaller than 150x150, content.Tag ends up being set to "Test" which is what you pull from when resized back to > 150x150



来源:https://stackoverflow.com/questions/4748059/setting-content-of-a-scatterviewitem-depending-on-the-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!