未分配DependencyProperty

时间:2012-06-29 16:52:02

标签: c# wpf dependency-properties

我创建了一个公开DependencyProperty的UserControl:

namespace MyApp.Sql
{
 public partial class SqlConnectionStringBuilder
 {
    private static readonly SqlConnectionString DefaultValue = new SqlConnectionString { IntegratedSecurity = true, Pooling = false };

    public static readonly DependencyProperty ConnectionStringProperty =
        DependencyProperty.Register("ConnectionString", typeof(SqlConnectionString),
                                    typeof (SqlConnectionStringBuilder),
                                    new FrameworkPropertyMetadata(
                                        DefaultValue, 
                                        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                        ConnectionStringChanged));



    private static void ConnectionStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var builder = (SqlConnectionStringBuilder) d;
        if (e.NewValue == null)
            builder.Dispatcher.BeginInvoke((Action)(() => d.SetValue(ConnectionStringProperty, DefaultValue)));
        else
            builder.RegisterNewConnectionString((SqlConnectionString)e.NewValue);
    }

    public SqlConnectionString ConnectionString
    {
        get { return (SqlConnectionString)GetValue(ConnectionStringProperty); }
        set { SetValue(ConnectionStringProperty, value); }
    }

    private void RegisterNewConnectionString(SqlConnectionString newValue)
    {
        if (newValue != null)
            newValue.PropertyChanged += ConnectionStringPropertyChanged;
    }
    ...
  }
}

现在我尝试在另一个UserControl中使用ConnectionString并将TextBlock附加到ConnectionString:

    

</UserControl.Resources>
<Grid Grid.IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <MyApp.Sql:SqlConnectionStringBuilder  ConnectionString="{Binding ElementName=_this, Path=ServerConnectionString}"  />
    <TextBlock Text="{Binding ElementName=_this, Path=ServerConnectionString, StringFormat='Produced Connection String: {0}'}"
               Grid.Row="1" />
</Grid>

namespace MyApp
{
  public partial class SqlProvider
  {
    public SqlProvider
    {
       InitializeComponent();
       DataContext = this;
    }

    private SqlConnection _connection;
    public SqlConnection ServerConnectionString
    {
        get { return _connection; }
        set
        {
            _connection = value;
           OnPropertyChanged("ServerConnectionString");
        }
    }


    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
  }
}

但是从未将SqlConnectionString分配给TextBlock。 当我调试ConnectionStringBuilder的SqlConnection属性时,它总是显示null

我的错在哪里?

2 个答案:

答案 0 :(得分:0)

检查你的绑定。我假设您正在使用SqlConnectionStringBuilder UserControl来创建连接,在这种情况下,它的绑定模式应该是双向的。

现在,您似乎正在尝试绑定到MainWindow的代码隐藏。如果将UserControl的ConnectionString绑定设置为双向,则对该依赖项属性所做的任何更改都将注入到MainWindow的ServerConnectionString属性中。并且,因为您的MainWindow实现了INotifyPropertyChanged,所以一旦更新了ServerConnectionString属性,TextBlock就应该更新。

此外,如果您绑定到MainWindow,则无需在绑定中指定ElementName属性。由于您在构造函数中设置了DataContext = this,因此您可以在UserControl上说{Binding Path=ServerConnectionString, Mode=TwoWay}

答案 1 :(得分:0)

如果我使用这样的ElementNames:

<MyApp.Sql:SqlConnectionStringBuilder x:Name="sqlConnectionStringBuilder"  ConnectionString="{Binding ElementName=_this, Path=ServerConnectionString, Mode=TwoWay}"  />
<TextBlock Text="{Binding ElementName=sqlConnectionStringBuilder, Path=ConnectionString, StringFormat='Produced Connection String: {0}'}" Grid.Row="1" />

它有效。

但为什么绑定到本地实例不起作用?