0

我创建了一个公开 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

我的错在哪里?

4

2 回答 2

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" />

有用。

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

于 2012-07-04T16:25:57.043 回答
0

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

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

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

于 2012-06-29T18:01:20.630 回答