WPF:绑定到我的自定义控件不会更新

3

我创建了一个用户控件来“浏览”文件,它基本上由一个文本框和一个按钮组成。

它有几个属性,允许我选择目录、现有文件(打开文件对话框)或不存在的文件(保存文件对话框),指定过滤器等。

我正在使用依赖属性,如下所示:

    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 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:实际上,如果我通过用户控件进行更改,则模型中的更改不会被复制 :/

编辑3:我在我的currentItem.FilePath-> UserControl的绑定中添加了"Mode=TwoWay",现在好像可以工作了,但为什么?具有相同绑定的TextBox却可以工作!


你的用户控件的特定部分是什么样子? - dowhilefor
@dowhilefor 我更新了我的问题,这是你想要的吗? - J4N
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,(TextBox控件的Text属性也是如此):

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

这样,当您绑定该属性时,就不必显式设置Mode=TwoWay


谢谢您的回复,我的问题主要是您的第二个建议。它解释了为什么文本框可以工作而我的控件不能。默认情况下,它是什么?OneWay? - J4N
是的,默认值为 OneWay。不过,我建议您删除多余的属性更改代码。 - Adi Lester
谢谢,我会的,有人告诉我这是必要的。 - J4N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接