将XAML属性值设置为用户控件

avi*_*viv 7 wpf binding wpf-controls

我在WPF中有一个用户控件,我希望从使用它的XAML中读取其中一个标签的文本.因此..

我的用户控制:

 <UserControl x:Class="muc">
        <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">          
             <Label.Content>
                <Binding ElementName="TestName" Path="." />
             </Label.Content>
        </Label>
 </UserControl>
Run Code Online (Sandbox Code Playgroud)

然后使用它:

 <mycontorls:muc TestName="This is a test" />
Run Code Online (Sandbox Code Playgroud)

但它不起作用......我怎样才能阅读这些属性?

spr*_*ite 8

我尝试了前两个答案以及我在代码中工作但不在XAML上的工作(在使用控件时也不会让你看到设计视图中的变化).

要使属性像任何其他本机一样工作,这里是完整的过程:(该示例添加Nullable类型的依赖项属性以在控件中显示为文本,如果为null则显示默认值)

  1. 在代码文件中:

    1.a定义依赖属性:

    public static readonly DependencyProperty MyNumberProperty = DependencyProperty.Register("MyNumber", typeof(Nullable<int>), typeof(MyUserControl), new PropertyMetadata(null, new PropertyChangedCallback(OnMyNumberChanged)));
    
    Run Code Online (Sandbox Code Playgroud)

    1.b实现OnMyNumberChanged回调:

    private static void OnMyNumberChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args){
        // When the color changes, set the icon color PlayButton
        MyUserControl muc = (MyUserControl)obj;
        Nullable<int> value = (Nullable<int>)args.NewValue;
        if (value != null)
        {
            muc.MyNumberTextBlock.Text = value.ToString();
        }
        else
        {
            muc.MyNumberTextBlock.Text = "N/A";
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    1.c实现MyNumber属性(不依赖)使用依赖属性以方便代码使用:

    public Nullable<int> MyNumber{
        get
        {
            return (Nullable<int>)GetValue(MyNumberProperty);
        }
        set
        {
            SetValue(MyNumberProperty, value);
            OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));    // Old value irrelevant.
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在XAML文件中,将TextBlock控件的文本绑定到属性(而不是依赖项)以获取依赖项属性的默认值,以防它未被控件的用户设置(假设您调用了用户控件的根元素"RootElement") "):

这段代码:

 < TextBlock Name="MyNumberTextBlock" Text="{Binding MyNumber, ElementName=RootElement}"/>
Run Code Online (Sandbox Code Playgroud)


Qua*_*ter 5

如果给根 UserControl 元素命名,则可以使用 ElementName 引用它:

<UserControl x:Class="muc"
             Name="rootElement">
    <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">
        <Label.Content>
            <Binding ElementName="rootElement" Path="TestName" />
        </Label.Content>
    </Label>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

您还可以使用标记扩展语法使其更短:

<UserControl x:Class="muc"
             Name="rootElement">
    <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold"
           Content="{Binding TestName, ElementName=rootElement}"/>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

还要记住,您的控件将在其属性设置之前创建。您将需要实现 INotifyPropertyChanged 或具有TestName依赖属性,以便在设置属性后重新评估绑定。


Alv*_*par 4

我只用 Silverlight 做过这个,但如果它以完全相同的方式工作,我不会感到惊讶!

// <summary>
// Xaml exposed TextExposedInXaml property.
// </summary>
public static readonly DependencyProperty TestNameProperty = DependencyProperty.Register("TestName", typeof(string), typeof(NameOfMyUserControl), new PropertyMetadata(string.empty));

// <summary>
 // Gets or sets the control's text
// </summary>
public string TextExposedInXaml
{
            get
            {
                return (string)GetValue(TestNameProperty );
            }

            set
            {
                SetValue(TestNameProperty , value);

                // set the value of the control's text here...!
            }
}
Run Code Online (Sandbox Code Playgroud)