3

ファイルを「参照」するためのユーザー コントロールを作成しました。基本的には、1 つのテキスト ボックスと 1 つのボタンで構成されています。

いくつかのプロパティがあり、ディレクトリ、既存のファイル (ファイルを開くダイアログ) または存在しないファイル (ファイルの保存ダイアログ) のいずれかを選択し、フィルターを指定することができます...

次のような依存関係プロパティを使用しています。

    public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
        "FilePath",
        typeof(String),
        typeof(BrowseFileControl),
        new PropertyMetadata(default(String), InvokeFilePathChanged)
    );
    public String FilePath { get { return (String)GetValue(FilePathProperty); } set { SetValue(FilePathProperty, value); } }

    private static void InvokeFilePathChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        BrowseFileControl view = (BrowseFileControl)property;
        view.InvokeFilePathChanged((String)args.OldValue, (String)args.NewValue);
    }
    protected virtual void InvokeFilePathChanged(String oldValue, String newValue)
    {
        InvokePropertyChanged("FilePath");
    }

私の見解では、編集する「構成」を選択できるリストボックスがあり、すべてのフィールド (ユーザーコントロールを含む) が CurrentConfiguration にバインドされています (CurrentConfiguration は SelectedItem にバインドされています)。

私の問題: 最初のロードは常に問題ありませんが、別の構成を選択すると、更新されず、古いテキストが保持されます。

私のバインディングは次のようなものです:

<userContols:BrowseFileControl  Grid.Row="4" Grid.Column="1" 
    Margin="2" FilePath="{Binding CurrentConfiguration.TagListFile, 
    ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
    IsFolder="False" Filter="All files (*.*)|*.*" CanBeInexistantFile="False"/>

まったく同じバインディングで単純な Textbox を使用すると、正しく更新されます!

<TextBox Grid.Row="4" Grid.Column="1" 
    Text="{Binding CurrentConfiguration.TagListFile,ValidatesOnDataErrors=true, NotifyOnValidationError=true}" 
    Margin="2"/>

Visual Studio の出力ウィンドウにもバインド エラーは表示されません。

では、私のバインディングの何が問題なのですか?

編集: UserControl の xaml:

<Grid DataContext="{Binding ElementName=uxBrowseFileControl}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Padding="2" Text="{Binding FilePath, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
    <Button Content="Browse" Grid.Column="1" Padding="2" Command="{Binding BrowseCommand}"/>
</Grid>

uxBrowseFileControl は、<UserControl>

編集 2: 実際、userControl を使用して何かを変更すると、モデルにも複製されません:/

Edit3:「Mode=TwoWay」を currentItem.FilePath->UserControl のバインディングに入れましたが、現在は機能しているようですが、なぜですか? 同じバインドの TextBox が機能していました!

4

1 に答える 1

5

DependencyProperty の変更コールバックを削除する必要があります。依存関係プロパティが変更されたときに UI を更新するために、特別なロジックは必要ありません。これは、依存関係プロパティに既に組み込まれています。

必要なのはこれだけです:

public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
    "FilePath",
    typeof(String),
    typeof(BrowseFileControl),
    new PropertyMetadata(default(String))
);

public String FilePath { get { return (String)GetValue(FilePathProperty); } set { SetValue(FilePathProperty, value); } }

TwoWay依存関係プロパティをデフォルトでバインドするように設定することもできます (コントロールのTextプロパティもこれを行います)。TextBox

public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
    "FilePath",
    typeof(String),
    typeof(BrowseFileControl),
    new FrameworkPropertyMetadata(default(String), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
);

Mode=TwoWayこれにより、そのプロパティをバインドするたびに明示的に設定する必要がなくなります。

于 2012-11-29T09:59:41.347 に答える