WP7自定义用户控件属性

时间:2010-10-26 16:53:08

标签: silverlight xaml windows-phone-7

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

MainPage 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,在内容中我尝试选择数据上下文元素值,然后向下滚动以选择“Title1” ,但是当我编译它时,按钮的内容永远不会改变。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

正如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" />