首页 > 解决方案 > 为什么要使用绑定和依赖属性?

问题描述

我有一个包含按钮的用户控件设计。我在其他地方使用它,并且必须在那里填写一些内容。现在我的问题是,当我更新这个用户控件时,我可以访问它的内容并且我可以填写它。问题是,我应该为此使用绑定吗?为什么?

 <Grid   Margin="2,5,2,5">
    <Border CornerRadius="5" BorderThickness="1" BorderBrush="Black">
        <Button x:Name="button" Margin="2, 4, 2, 4"
                Content="{Binding content}" 
                Click="button_Click" 
                Background="Transparent" 
                Foreground="Black"
                Height="23"
                FontSize="10"
                Cursor="Hand" Padding="1"
                BorderThickness="0" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave"></Button>
    </Border>
</Grid>



public string content 
    {
        get { return (string)GetValue(contentProperty); }
        set { SetValue(contentProperty, value); }
    }
    public static readonly DependencyProperty contentProperty =
               DependencyProperty.Register("content", typeof(string), typeof(ButtonView), new PropertyMetadata(""));

标签: c#wpf-controls

解决方案


推荐阅读