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

バインディングをチェックしてください。SqlConnectionStringBuilder UserControl を使用して接続を作成していると仮定します。その場合、バインディング モードは双方向である必要があります。

現在、MainWindow のコード ビハインドにバインドしようとしているようです。UserControl の ConnectionString バインディングを双方向に設定すると、その依存関係プロパティに加えられた変更はすべて MainWindow の ServerConnectionString プロパティに注入されます。また、MainWindow は INotifyPropertyChanged を実装しているため、ServerConnectionString プロパティが更新されると、TextBlock も更新されます。

また、MainWindow にバインドする場合、バインディングで ElementName プロパティを指定する必要はありません。コンストラクターで を設定しているので、UserControlDataContext = thisで言うことができます。{Binding Path=ServerConnectionString, Mode=TwoWay}

于 2012-06-29T18:01:20.630 に答える
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 に答える