代码之家  ›  专栏  ›  技术社区  ›  Jiew Meng

绑定不提交?

  •  3
  • Jiew Meng  · 技术社区  · 14 年前

    我有一个 TabControl 绑定到一个 ICollectionView with derives from ObservableCollection<editortabview model> 。我认为相当标准的MVVM多文档模式?总之, editortabview model has a property content that contains the string to be displayed.我发现装订工作正常…

    //add 2 default tab for a test,also set their content property to the responding values…(为测试添加2个默认选项卡,也将其内容属性设置为各自的值…)
    _ tabs.add(new editortabview模型content=“tab 1”);
    _ tab.add(new editortabview模型content=“tab 2”);
    < /代码> 
    
    

    其值已正确呈现

    xaml

    <!--要呈现editortabview模型的数据模板-->
    <datatemplate datatype=“x:type vm:editortabview模型”>
    <me:markdowneditor(我:Markdowneditor)
    textcontent=“绑定路径=content.content,relativesource=relativesource模式=templatedParent,模式=twoway”
    options=“绑定路径=选项,relativesource=relativesource ancestortype=x:类型窗口”/>gt;
    </datatemplate>
    < /代码> 
    
    

    结果

    但当我更改值、切换选项卡并返回时,我再次在构造函数中获取字符串集…在中显示

    Visual Studio解决方案.ICollectionView具有派生自ObservableCollection<EditorTabViewModel>. 我认为相当标准的MVVM多文档模式?不管怎样,EditorTabViewModel有财产Content包含要显示的字符串的。我发现装订工作正常…

    // Add 2 default tabs for a test, also set their Content property to the respective values ...
    _tabs.Add(new EditorTabViewModel { Content = "Tab 1" });
    _tabs.Add(new EditorTabViewModel { Content = "Tab 2" });
    

    其值已正确呈现

    XAML

    <!-- DataTemplate to render EditorTabViewModels -->
    <DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
        <me:MarkdownEditor 
            TextContent="{Binding Path=Content.Content, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" 
            Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
    </DataTemplate>
    

    结果

    但当我更改值、切换选项卡并返回时,我再次在构造函数中获取字符串集…显示在this video (on screenr)

    这个Visual Studio Solution

    2 回复  |  直到 14 年前
        1
  •  0
  •   Fredrik Hedblad    14 年前

    将markdowneditor.xaml中文本框“txtitor”的updateSourceTrigger更改为propertyChanged。文本框的默认UpdateSourceTrigger值为LostFocus,并且在更改选项卡时从不引发该事件。这就是为什么它会恢复到以前的值

    <TextBox Grid.Row="1" x:Name="txtEditor" AcceptsReturn="True"
             Text="{Binding TextContent, UpdateSourceTrigger=PropertyChanged}" 
             FontFamily="{Binding Path=Options.FontFamily}"
             FontSize="{Binding Path=Options.FontSize}"
             FontWeight="{Binding Path=Options.FontWeight}"
             Background="{Binding Path=Options.Background}"
             Foreground="{Binding Path=Options.Foreground}" />
    
        2
  •  0
  •   alpha-mouse    14 年前

    我想是吧 MarkdownEditor.TextContent 属性不会告诉任何人它的值已更改,因此绑定机制不需要将它的新值写入 EditorTabViewModel.Content . 如果 TextContent 是的依赖属性 MarkdownEditor 您能确保它从您用来实际编辑文本的控件接收更改的文本吗?( TextBox 还是什么?