2016-01-20 117 views
3

我已经搜索谷歌和stackoverflow很多找到没有任何运气的问题的答案。我找到了解决问题的办法。例如:WPF嵌套数据绑定到控制 - 为什么它不起作用

Data Binding to Nested Properties?

但我已经知道了解决方案。我想知道为什么wpf不支持控件上的嵌套/虚线数据绑定。

解决方案是将父控件的DataContext设置为父数据对象,在我的情况下我的控制器/窗口datacontext上的ViewModel属性。所以我可以设置我的网格的DataContext和我的代码将工作,如果我改变我的TextBox绑定只使用Name属性。

另一种解决方案是在我的文本框中明确地设置UpdateSourceTrigger,并将我的嵌套数据绑定在TextBox控件上,如下所示。

但是为什么?为什么WPF不支持像下面这样做的嵌套绑定,而没有将UpdateSourceTrigger设置为显式?我想知道:)。

我有这样的文本框控件:

<Window> 
    <Grid> 
     <StackPanel> 
      <Label Content="Name" FontWeight="Bold"/> 
      <TextBox x:Name="NameTextBox" Text="{Binding Path=CreateEditAssetViewModel.Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Width="475" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Margin="0, 5" /> 
     </StackPanel> 
    </Grid> 
</Window> 

我的窗口datacontext的必然是这样的:

var createEditWindow = new CreateEditWindow(); 
var createEditController = new CreateEditWindowController(); 
createEditWindow.DataContext = createEditController; 
createEditWindow.Show(); 

我的控制器看起来是这样的:

public class CreateEditWindowController : ViewModelBase, ICreateEditWindowController 
{ 
    private ICreateEditWindowViewModel _createEditWindowViewModel; 
    public ICreateEditWindowViewModel CreateEditAssetViewModel 
    { 
     get { return _createEditWindowViewModel; } 
     set 
     { 
      if (_createEditWindowViewModel == value) return; 
      _createEditWindowViewModel = value; 
      OnPropertyChanged(nameof(CreateEditAssetViewModel)); 
     } 
    } 
} 

我与名称视图模型文本框控件绑定的属性如下所示:

public class CreateEditWindowViewModel : ViewModelBase, ICreateEditWindowViewModel 
{ 

    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (_name == value) return; 
      _name = value; 
      OnPropertyChanged(nameof(Name)); 
     } 
    } 
} 

而且我ViewModelBase:

public class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

请参阅您提到的链接。 –

回答

0

WPF做支持点属性,但是你总是要指定控制数据的上下文。

+0

您是否有任何官方资源从Microsoft解释这一点?我似乎无法找到任何:( – Poku

0

我认为wpf支持嵌套/虚线数据绑定。我在你提到的链接中发布了一个答案。 其中

<TextBox Text="{Binding Path=MyModel.MyCounter.CurrentNumber}"/> 

绑定工作正常。不在这里,但我已经做了很多例子,嵌套属性需要绑定到某些控件属性。事实上,WPF必须支持这种类型的绑定,否则提供单独的DataContext来分离控制将是非常困难的工作。

一些例子:

1.

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type iDP:DataRecordCellArea}},Path=Record.DataItem.IsParentRow}" Value="true"> 
<Setter Property="IsEnabled" Value="False"/> 

2。

<CheckBox HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          Cursor="Arrow" 
          IsChecked="{Binding Path=DataItem.IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type view:DeleteSubLocationsView}},Path=DataContext.ImportWizardViewModel.ContextObject.IsRQSReviewFieldChecked}"> 

example how nested binding work

1

这里的https://msdn.microsoft.com/en-us/library/ms752347%28v=vs.100%29.aspx

看那提供视觉反馈节,这是一个触发绑定属性,但他们用虚线财产

同为收集视图 section sub - >如何创建视图

他们在Application对象上使用了虚线属性。

在调试GUI时查看输出视图,如果WPF未在Data上下文中找到属性,则会抛出日志!

+0

WPF找到属性,它将属性中的数据绑定到视图,但它不启用双向绑定当我使用虚线属性 – Poku

+0

当然,您可以添加一个双向绑定 –