DependencyProperty绑定问题

J4N*_*J4N 1 .net c# wpf mvvm-light

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

<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>
Run Code Online (Sandbox Code Playgroud)

使用以下代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

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

(SelectedFile是使用此控件的usercontrol的ViewModel的属性)

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

如果我将绑定模式设置为TwoWay,我得到了以下异常:

An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module.
Run Code Online (Sandbox Code Playgroud)

那我做错了什么?

Cle*_*ens 5

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

DataContext = this;
Run Code Online (Sandbox Code Playgroud)

您不应该这样做,因为它会破坏任何基于DataContext的Bindings,例如,在继承的DataContext值中打破视图模型实例.

相反,你会改变UserControl的XAML中的绑定,如下所示:

<TextBox Text="{Binding SelectedFile,
                RelativeSource={RelativeSource AncestorType=UserControl}}" />
Run Code Online (Sandbox Code Playgroud)

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

<userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" />
Run Code Online (Sandbox Code Playgroud)

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