【问题标题】:WP7 silverlight custom control using popupWP7 silverlight 自定义控件使用弹出
【发布时间】:2010-10-26 10:00:43
【问题描述】:

我正在创建一个自定义日期选择器,我有一个文本框,一旦单击它就会在弹出窗口中打开一个日历。 我想要做的是改变弹出窗口的大小,以便它显示我的整个日历,但我无法改变它......,我尝试使用高度,宽度,MinHeight,MinWidth......但它没有不行,弹出窗口一直以固定大小显示。

问题是我的弹出窗口的父属性没有被评估,因为它有表达式问题(根据调试器),所以我确定我的弹出窗口的父级不是主屏幕(比如布局网格)。

例如,我怎样才能让我的弹出窗口在特定上下文中打开? 我的这部分代码不是 XAML,它只是 C# 代码,它看起来像:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CalendarBranch.components
{
    public class wpDatePicker:TextBox
    {
        private CalendarPopup calendar;
        private Popup popup;

        public wpDatePicker()
        {
            this.calendar = new CalendarPopup();
            this.popup = new Popup();

            this.popup.Child = this.calendar;
            this.popup.Margin = new Thickness(0);

            this.MouseLeftButtonUp += new MouseButtonEventHandler(wpDatePicker_MouseLeftButtonUp);

            this.calendar.onDateSelect += new EventHandler(onDateSelected);

            this.IsReadOnly = true;

        }

        protected void wpDatePicker_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.popup.Height = this.calendar.Height;
            this.popup.Width = this.calendar.Width;
            this.popup.HorizontalAlignment = HorizontalAlignment.Center;
            this.popup.VerticalAlignment = VerticalAlignment.Center;
            this.popup.HorizontalOffset = 0;
            this.popup.VerticalOffset = 0;
            this.popup.MinHeight = this.calendar.Height;
            this.popup.MinWidth = this.calendar.Width;

            this.popup.IsOpen = true;
        }

        private void onDateSelected(Object sender, EventArgs ea) {
            this.Text = this.calendar.SelectedValue.ToShortDateString();
            this.popup.IsOpen = false;
        }

    }
}

PS:Calendar 类只是一个 UserControl,它包含一个包含多个列、HyperLinkBut​​tons 和 TextBlocks 的网格,所以没什么特别的。

提前谢谢你们;)

干杯 米卢德 B.

【问题讨论】:

  • 我建议您快速阅读一下 Markdown 文档(编辑问题时,右侧有一个概要)。
  • 好的。 我的问题现在呢?

标签: c# silverlight windows-phone-7


【解决方案1】:

弹出控件调整自身大小以适应其中的内容。例如,如果您将 Popup 的子级设置为 StackPanel,宽度/高度设置为 100,则弹出窗口将为 100x100。

因此,不要设置弹出窗口的大小,而是设置内部面板的大小,这一点非常重要。尝试将您的内容包装到堆栈面板中并在那里分配必要的宽度/高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多