WPF用户控件绑定数据到用户控件属性

25

我有一个用户控件:

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 中绑定用户控件属性,并将其绑定到用户控件的按钮内容属性。如何实现?
1个回答

45
你可以尝试在用户控件内使用元素绑定。只需为UserControl命名并绑定属性即可:
<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>

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

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

16
如果您想了解如何使它更正确,我强烈推荐链接 - newman
你如何指定用户控件应该将哪种类型的VM视为参考数据?想象一下,如果VM不在相同的命名空间中等情况。 - st35ly
1
问题是 - 在自己的 XAML 中绑定用户控件属性,当前示例不使用 MVVM 模式,如果需要在用户控件中使用 MVVM 并绑定视图模型,则必须将用户控件的 DataContext 设置为视图模型实例,或者如果需要类型引用,请考虑使用 DataTemplate: https://dev59.com/8XA75IYBdhLWcg3ws7Yi - vitaliy zadorozhnyy
@newman的链接现在已经失效了,不过还是谢谢你 :) - infografnet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接