DependencyProperty 绑定问题 [英] Issue with DependencyProperty binding

查看:34
本文介绍了DependencyProperty 绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小的文件浏览器控件:

I created a small File Browser Control:

<UserControl x:Class="Test.UserControls.FileBrowserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="44" d:DesignWidth="461" Name="Control">
    <Grid Margin="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox  Margin="3" Text="{Binding SelectedFile}" IsReadOnly="True" TextWrapping="Wrap" />
        <Button HorizontalAlignment="Right" Margin="3" Width="100" Content="Browse" Grid.Column="1" Command="{Binding BrowseCommand}" />
    </Grid>
</UserControl>

后面有以下代码:

public partial class FileBrowserControl : UserControl
{
    public ICommand BrowseCommand { get; set; }
    //The dependency property
    public static DependencyProperty SelectedFileProperty = DependencyProperty.Register("SelectedFile",
        typeof(string),typeof(FileBrowserControl), new PropertyMetadata(String.Empty));
    public string SelectedFile { get{ return (string)GetValue(SelectedFileProperty);}  set{ SetValue(SelectedFileProperty, value);}}
    //For my first test, this is a static string
    public string Filter { get; set; }

    public FileBrowserControl()
    {
        InitializeComponent();
        BrowseCommand = new RelayCommand(Browse);
        Control.DataContext = this;
    }
    private void Browse()
    {
        SaveFileDialog dialog = new SaveFileDialog();
        if (Filter != null)
        {
            dialog.Filter = Filter;
        }
        if (dialog.ShowDialog() == true)
        {
            SelectedFile = dialog.FileName;
        }
    }
}

我是这样使用的:

<userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" Filter="XSLT File (*.xsl)|*.xsl|All Files (*.*)|*.*"/>

(SelectedFile 是使用该控件的用户控件的 ViewModel 的属性)

(SelectedFile is Property of the ViewModel of the usercontrol using this control)

目前的问题是,当我单击浏览"时,用户控件中的文本框正在正确更新,但未设置 viewmodel 父控件的 SelectedFile 属性(未调用 set 属性).

Currently the issue is that when I click on Browse, the textbox in the usercontrol is correctly updating, but the SelectedFile property of the viewmodel parent control is not set(no call to the set property).

如果我将绑定的 Mode 设置为 TwoWay,则会出现此异常:

If I set the Mode of the binding to TwoWay, I got this exception:

An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module.

那我做错了什么?

推荐答案

主要问题是您在其构造函数中将 UserControl 的 DataContext 设置为自身:

The primary problem is that you set your UserControl's DataContext to itself in its constructor:

DataContext = this;

您不应该这样做,因为它会破坏任何基于 DataContext 的绑定,即通过 DataContext 属性的属性值继承提供的视图模型实例

You should not do that, because it breaks any DataContext based Bindings, i.e. to a view model instance that is provided by property value inheritance of the DataContext property

相反,您可以像这样更改 UserControl 的 XAML 中的绑定:

Instead you would change the binding in the UserControl's XAML like this:

<TextBox Text="{Binding SelectedFile,
                RelativeSource={RelativeSource AncestorType=UserControl}}" />
    

现在,当您使用 UserControl 并编写类似的绑定

Now, when you use your UserControl and write a binding like

<userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" />

SelectedFile 属性绑定到视图模型中的 SelectedFile 属性,该属性应该位于从父控件继承的 DataContext 中.

the SelectedFile property gets bound to a SelectedFile property in your view model, which should be in the DataContext inherited from a parent control.

这篇关于DependencyProperty 绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆