question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UserControl data binding regression in 2.0.0

See original GitHub issue

Description

Binding the DataContext of a UserControl using an ElementName binding worked in 0.9.9, does not work in 2.0.0

Repro steps

MainWindow.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:ViewModels;assembly=FsEmptyWindowsApp1">
    <StackPanel>
        <TextBox Name="Foo"/>
        <local:TestControl DataContext="{Binding ElementName=Foo, Path=Text}"/>
    </StackPanel>
</Window>

TestControl.xaml

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <TextBox Text="{Binding .}"/>
</UserControl>

Expected behavior

Typing into the TextBox Foo results in the same text in the TestControl.

Actual behavior

TestControl is blank. Binding error in Debug Output is

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘ElementName=Foo’. BindingExpression:Path=Text; DataItem=null; target element is ‘TestControl’ (Name=‘’); target property is ‘DataContext’ (type ‘Object’)

Known workarounds

None.

Related information

  • Windows 8.1
  • FsXaml 2.0.0 via nuget
  • .NET 4.5

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ReedCopseycommented, May 19, 2016

This has now been solved in 2.1. @mbergin please try https://www.nuget.org/packages/FsXaml.Wpf/2.1.0

It should now work with the ElementName bindings (as well as your x:Reference workaround).

2reactions
ruxocommented, May 19, 2016

@ReedCopsey I found that System.Windows.Markup.IComponentConnector plays an important role here (but I don’t know exactly how).

By implementing our UI class with this interface and the binding will work.

Example (sorry, haven’t tried the new library yet):

open RZ.Wpf.CodeBehind

type TestControl() as me =
  inherit UserControl()

  let mutable content_loaded = false

  do (me :> IComponentConnector).InitializeComponent()

  interface IComponentConnector with
    member x.InitializeComponent() =
      if not content_loaded then
          me.InitializeCodeBehind "testcontrol.xaml"

      content_loaded <- true

    member x.Connect(connection_id, target) = content_loaded <- true

(In case you might wonder, this I copied from C# WPF generated code 😄 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data binding to a UserControl in WPF
I have a UserControl that I want to participate in data binding. I've set up the dependency properties in the user control, but...
Read more >
Create user controls that support simple data binding
This walkthrough creates a simple data-bindable control from a User Control. Add a User Control item to the SimpleControlWalkthrough project:.
Read more >
Data Widgets
9320 - Regression: If DataGrid is bound to table containing 01Oct99 and NumberFormat for column is ddmmmyy, date displays incorrectly as 10Jan99. 9318...
Read more >
Search Results - CVE
The mission of the CVE® Program is to identify, define, and catalog publicly disclosed cybersecurity vulnerabilities.
Read more >
vegan: Community Ecology Package
A constrained ordination object from cca, rda or capscale. ... metaMDS uses monoMDS as its NMDS engine from vegan version 2.0-0, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found