WPF 用户控件将数据绑定到用户控件属性 [英] WPF user control bind data to user control property

查看:53
本文介绍了WPF 用户控件将数据绑定到用户控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户控制权:

xaml

<UserControl x:Class="controlmaker.checkButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="114" d:DesignWidth="221">
<Grid Background="Aqua" >
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="58,24,0,0" Name="checkBox1" VerticalAlignment="Top" />
    <Button Content="{Binding buttText}" Height="23" HorizontalAlignment="Left" Margin="58,57,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
</UserControl>

后面的代码

public partial class checkButton : UserControl
{
    public checkButton()
    {
        InitializeComponent();
    }


    public static readonly DependencyProperty buttTextProperty =
DependencyProperty.Register("buttText", typeof(String),
typeof(checkButton), new FrameworkPropertyMetadata(string.Empty));

    public String buttText
    {
        get { return GetValue(buttTextProperty).ToString(); }
        set { SetValue(buttTextProperty, value); }
    }


}

和主窗口 xaml

<Window x:Class="controlmaker.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:controlmaker">
<Grid>
    <my:checkButton buttText="aka"  HorizontalAlignment="Left" Margin="145,115,0,0" x:Name="checkButton1" VerticalAlignment="Top" Height="133" Width="250" />
</Grid>
</Window>

我想在窗口 xaml 中绑定用户控件属性并将其在用户控件中绑定到按钮内容属性.怎么做?

I want to bind user control property in window xaml and bind it in user control to button content property. How to do it?

推荐答案

您可以尝试在用户控件中绑定元素.只需为 UserControl 命名并绑定属性:

You can try Element binding inside the user control. Just give a name to UserControl and bind property:

<UserControl x:Class="controlmaker.checkButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="114" d:DesignWidth="221"
         x:Name="MyUserControl">
<Grid Background="Aqua" >
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left"
          Margin="58,24,0,0" Name="checkBox1" VerticalAlignment="Top" />
    <Button Content="{Binding Path=buttText, ElementName=MyUserControl}"
          Height="23" HorizontalAlignment="Left" Margin="58,57,0,0" 
          Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
</UserControl>

然后您可以从用户控件使用位置绑定或放置静态文本

And then you can Bind or put static text from user control usage place

<my:checkButton buttText="aka" />

  <my:checkButton buttText="{Binding SomeProperty}" />

这篇关于WPF 用户控件将数据绑定到用户控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆