绑定设置属性,但界面没有更新。我引用的项目/控制范围内的调试? [英] Binding Setting Property but UI not updating. Can I debug within referenced project/control?

查看:104
本文介绍了绑定设置属性,但界面没有更新。我引用的项目/控制范围内的调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似下面绑定的自定义控件

I have a custom control with bindings like below

<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
    <me:MarkdownEditor 
        Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType=Window}}" />
</DataTemplate>

我发现,绑定( Window1.Options )被设置(通过code在调试模式下步进后),降价编辑器选项(应该设置字体,颜色等)不被设置,或至少在UI不更新。我想什么错误与在 MarkdownEditor.xaml.cs 但那是另一个(引用)的项目发生。我如何可以验证 MarkdownEditor.Options 被至少设置?

I find that binding (Window1.Options) is being set (after stepping through code in debug mode), the markdown editor options (supposed to set Fonts, Colors etc) does not get set, or at least the UI does not update. I want to bug whats happening with in the MarkdownEditor.xaml.cs but thats another (referenced) project. How can I verify that the MarkdownEditor.Options is being set at least?

我实际测试了 MarkdownEditor 侧是由以下

I have actually tested that the MarkdownEditor side is working by the below

<Window ...>
    <Grid>
        <Button Content="Options" Click="Button_Click" Grid.Row="0" />
        <me:MarkdownEditor Options="{Binding Options, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Row="1" />
    </Grid>
</Window>

所以,不同的是后者是 MarkdownEditor 只是在一个电网窗口。一个失败是一个 MarkdownEditor 中的的TabControl 绑定到的ObservableCollection&LT; TabViewModel&GT;

So the difference is the latter is a MarkdownEditor just in a Grid in a Window. The one failing is a MarkdownEditor within a TabControl bound to a ObservableCollection<TabViewModel>

Visual Studio解决方案复制问题

我在解释的事情不是真的好,所以一个简单的项目我做了减去所有上传到媒体火 所以你可以看看什么是错

I am not really good at explaining things, so a simple project I made up minus all the unnecessary noise uploaded to media fire so you can take a look at whats wrong

表现出对问题的视频 Screenr

The video showing the problem on Screenr

只需简单的使用,编辑在一个窗口/格。

With just a simple usage, editor in a window/grid.

绑定工程确定

然后连同的TabControl 使用时,势必的ObservableCollection&LT; EditorTabViewModel&GT; ,如图中的绑定工作, 2 文本框上课更新其值。但编辑不更新

Then when used in conjunction with TabControl bound to ObservableCollection<EditorTabViewModel>, the binding works as shown in the 2 TextBoxes updating its values. but the editor does not update

推荐答案

阅读肯特Boogaart的回答<一后href=\"http://stackoverflow.com/questions/4230698/whats-the-difference-between-dependency-property-setvalue-setcurrentvalue\">this问题我认为要改变的SetValue到SetCurrentValue正确的地方是不是在CLR属性,但在构造MarkDownEditor。

After reading Kent Boogaart's answer to this question I think that the right place to change SetValue to SetCurrentValue isn't in the CLR Property but in the constructor for MarkDownEditor.

public MarkdownEditor()
{
    InitializeComponent();
    //Options = new MarkdownEditorOptions();
    this.SetCurrentValue(OptionsProperty, new MarkdownEditorOptions());
    DataContext = this;
}

在事实上,这将工作一样好,没有也this.SetCurrentValue自选项将通过绑定来设置。

In fact, this will work just as good without this.SetCurrentValue also since Options will be set through the Binding.

要验证您的绑定实际上已经通过的SetValue覆盖,你可以在某些事件TabUsage添加此code(如previewMouseRightButtonDown为字号文本框)和绑定将开始重新工作。

To verify that your Binding has in fact been overwritten by SetValue you can add this code in some event for TabUsage (e.g PreviewMouseRightButtonDown for the FontSize TextBox) and the Binding will start to work again.

private void TextBox_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    MarkdownEditor.MarkdownEditor editor = VisualTreeHelpers.GetVisualChild<MarkdownEditor.MarkdownEditor>(this);
    Binding binding = new Binding();
    binding.Path = new PropertyPath("Options");
    binding.Source = this;
    binding.Mode = BindingMode.TwoWay;
    editor.SetBinding(MarkdownEditor.MarkdownEditor.OptionsProperty, binding);
}

这篇关于绑定设置属性,但界面没有更新。我引用的项目/控制范围内的调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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