Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding using a specified source fails #9375

Closed
eriklimakc opened this issue Feb 29, 2024 · 1 comment
Closed

Binding using a specified source fails #9375

eriklimakc opened this issue Feb 29, 2024 · 1 comment
Labels
area-Binding closed-ByDesign Described behavior is by design. team-Markup Issue for the Markup team

Comments

@eriklimakc
Copy link

Describe the bug

When attempting to bind data from the ViewModel to a Custom UserControl created entirely in C# the binding fails and no data appears.

Steps to reproduce the bug

Repro app: FrameControl.zip

MainWindow.xaml

<StackPanel x:Name="RootElement" 
            Orientation="Horizontal"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

MainWindow.xaml.cs

public MainWindow()
{
	this.InitializeComponent();

	RootElement.DataContext = new MainPageViewModel();

	var frameControl = new FrameControl();
	frameControl.SetBinding(FrameControl.CaptionProperty, new Binding
	{
		Path = new PropertyPath(nameof(MainPageViewModel.Title))
	});

	RootElement.Children.Add(frameControl);
}

MainPageViewModel

public partial class MainPageViewModel : ObservableObject
{
	[ObservableProperty]
	private string _title = "Hello!";
}

FrameControl

public partial class FrameControl : UserControl
{
	public string Caption
	{
		get { return (string)GetValue(CaptionProperty); }
		set { SetValue(CaptionProperty, value); }
	}

	public static readonly DependencyProperty CaptionProperty =
		DependencyProperty.Register(nameof(Caption), typeof(string), typeof(FrameControl), new PropertyMetadata(""));

	public FrameControl()
	{
		var grid = new Grid();
		grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
		grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

		var border = new Border
		{
			Width = 300,
			Background = new SolidColorBrush(Colors.Blue)
		};
		Grid.SetColumn(border, 0);

		var textBlock = new TextBlock
		{
			FontSize = 30,
			Foreground = new SolidColorBrush(Colors.White)
		};
		textBlock.SetBinding(TextBlock.TextProperty, new Binding
		{
			Path = new PropertyPath(nameof(Caption)),
			Source = this,
			Mode = BindingMode.OneWay
		});
		Grid.SetColumn(textBlock, 1);

		grid.Children.Add(border);
		grid.Children.Add(textBlock);

		Content = grid;
	}
}

Expected behavior

The source binding should work.

Screenshots

Current

image

Expected

image

NuGet package version

None

Packaging type

No response

Windows version

No response

IDE

No response

Additional context

No response

@bpulliam bpulliam transferred this issue from microsoft/WindowsAppSDK Feb 29, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Feb 29, 2024
@bpulliam bpulliam added area-Binding team-Markup Issue for the Markup team and removed needs-triage Issue needs to be triaged by the area owners labels Feb 29, 2024
@evelynwu-msft
Copy link
Contributor

@eriklimakc Because FrameControl ultimately inherits from DependencyObject it needs to follow the rules for a DependencyObject. Specifically, it needs to have type information that can be located through the app's (generated by default) IXamlMetadataProvider implementation. If FrameControl were referenced directly in XAML markup then this type information would be automatically included in the IXamlMetadataProvider implementation but because it is not you need to decorate the type with the [Bindable] attribute in order to force its inclusion. Note that MainPageViewModel does not have the same requirement because it does not inherit from DependencyObject.

@llongley llongley closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2024
@llongley llongley added the closed-ByDesign Described behavior is by design. label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding closed-ByDesign Described behavior is by design. team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

4 participants