【问题标题】:WP7 Custom User Control PropertyWP7 自定义用户控件属性
【发布时间】:2010-10-26 16:53:08
【问题描述】:

我想要一个名为 Segmented Panel 的用户控件,其中包含 3 个按钮,我想从 XAML 屏幕修改它们的名称 (Title1/2/3)。

主页 XAML:

<local:SegmentedControl HorizontalAlignment="Left" Margin="43,62,0,0" x:Name="segmentedControl1" VerticalAlignment="Top" Title1="ENTER BUTTON NAME HERE" Title2="blah.." Title3="Last Button" Loaded="segmentedControl1_Loaded" />

SegmentedControl XAML.CS:

public static readonly DependencyProperty Title1Property
        = DependencyProperty.RegisterAttached("Title1", typeof(String), typeof(StackPanel),
        new PropertyMetadata(""));

    public static void SetTitle1(UIElement element, String value) {
        element.SetValue(Title1Property, value);
    }
    public static string GetTitle1(UIElement element) {
        return (string)element.GetValue(Title1Property);
    }

    public string Title1 {
        get { return GetValue(Title1Property).ToString(); }
        set { SetValue(Title1Property, value); }
    }

在表达式混合中,我打开分段面板,选择 button1,在内容中我尝试选择 Data ContextElement Value,然后向下滚动以选择“Title1” ,但是当我编译它时,按钮的内容永远不会改变。

谁能帮帮我?

【问题讨论】:

  • 我不明白。您正在向 StackPanel 添加附加属性,但您的类型是 SegmentedControl?你的意思是在 typeof(SegmentedControl) 上声明你的依赖属性吗?

标签: silverlight xaml windows-phone-7


【解决方案1】:

正如 John Gardner 所建议的,您必须在依赖属性实现中编写 ...typeof(SegmentedControl),然后必须将该属性绑定到适当的 UI 元素!

例如:

<local:SegmentedControl HorizontalAlignment="Left" Margin="43,62,0,0" x:Name="segmentedControl1" VerticalAlignment="Top" Title1="{Binding Title1}" ... Loaded="segmentedControl1_Loaded" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2012-02-18
    相关资源
    最近更新 更多