【问题标题】:WPF: Binding to my custom control doesn't updateWPF:绑定到我的自定义控件不会更新
【发布时间】:2012-11-17 08:26:04
【问题描述】:

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

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

我正在使用这样的依赖属性:

    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 是&lt;UserControl&gt;的名称

编辑 2:事实上,如果我通过 userControl 更改某些内容,它也不会在模型中复制:/

Edit3:我在 currentItem.FilePath->UserControl 的绑定中加入了“Mode=TwoWay”,现在似乎可以使用了,但为什么呢?具有相同绑定的 TextBox 正在工作!

【问题讨论】:

  • 用户控件的特定部分看起来如何?
  • @dowhilefor 我更新了我的问题,这是你想要的吗?

标签: wpf data-binding binding user-controls dependency-properties


【解决方案1】:

您应该删除 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。无论如何,我建议您删除多余的属性更改代码。
  • 谢谢,我会的,有人告诉我这是必要的。
猜你喜欢
  • 2015-06-03
  • 2023-03-13
  • 2018-04-09
  • 2023-03-31
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多