Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wpf - Binding Setting Property but UI not updating. Can I debug within referenced project/control?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...