绑定设置属性但 UI 未更新.我可以在引用的项目/控件中进行调试吗? [英] Binding Setting Property but UI not updating. Can I debug within referenced project/control?

查看:12
本文介绍了绑定设置属性但 UI 未更新.我可以在引用的项目/控件中进行调试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有如下绑定的自定义控件

<DataTemplate DataType="{x:Type vm:EditorTabViewModel}"><me:MarkdownEditorOptions="{Binding Path=Options,RelativeSource={RelativeSource AncestorType=Window}}"/></数据模板>

我发现绑定 (Window1.Options) 正在设置(在调试模式下单步执行代码之后),markdown 编辑器选项(应该设置字体、颜色等)没有设置,或者至少 UI 没有更新.我想检查 MarkdownEditor.xaml.cs 中发生的事情,但那是另一个(引用的)项目.如何验证是否至少设置了 MarkdownEditor.Options?

我已经实际测试了 MarkdownEditor 端是由下面的工作

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

所以区别在于后者是 MarkdownEditor 只是在一个 Window 中的一个 Grid 中.一个失败是 TabControl 中的 MarkdownEditor 绑定到 ObservableCollection

Visual Studio 解决方案复制问题

我不太擅长解释事情,所以我做了一个简单的项目,减去上传到 question 我认为将 SetValue 更改为 SetCurrentValue 的正确位置不在 CLR 属性中,而是在 MarkDownEditor 的构造函数中.

public MarkdownEditor(){初始化组件();//Options = new MarkdownEditorOptions();this.SetCurrentValue(OptionsProperty, new MarkdownEditorOptions());数据上下文 = 这个;}

事实上,没有 this.SetCurrentValue 也一样好,因为 Options 将通过 Binding 设置.

要验证您的 Binding 实际上已被 SetValue 覆盖,您可以在 TabUsage 的某些事件中添加此代码(例如,FontSize TextBox 的 PreviewMouseRightButtonDown)并且 Binding 将再次开始工作.

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

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>

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?

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>

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 Solution Replicating The Problem

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

The video showing the problem on Screenr

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

the binding works ok

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

解决方案

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;
}

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

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);
}

这篇关于绑定设置属性但 UI 未更新.我可以在引用的项目/控件中进行调试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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