Skip to content

VB -> C#: XAML Bindings don't work properly #1169

@nick2893

Description

@nick2893

When I begin with the following in VB:

<Window x:Class="MainWindow" x:Name="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <Grid>
        <TextBlock Height="100" Background="{Binding ElementName=MainWindow, Path=AwesomeColor}"/>
    </Grid>
</Window>
Class MainWindow


    Public Property AwesomeColor As Brush
        Get
            Return GetValue(AwesomeColorProperty)
        End Get
        Set(value As Brush)
            SetValue(AwesomeColorProperty, value)
        End Set
    End Property

    Public Shared AwesomeColorProperty As DependencyProperty = DependencyProperty.Register(
        "AwesomeColor", GetType(Brush), GetType(MainWindow), New PropertyMetadata(Brushes.Yellow))


End Class

It is converted to the following in XAML:

<Window x:Class="MainWindow" x:Name="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <Grid>
        <TextBlock Height="100" Background="{Binding ElementName=MainWindow, Path=AwesomeColor}"/>
    </Grid>
</Window>

However, the code will not compile; instead, it should be as follows:

<Window x:Class="WpfApp3.MainWindow" x:Name="MainWindowRoot"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <Grid>
        <TextBlock Height="100" Background="{Binding ElementName=MainWindowRoot, Path=AwesomeColor}"/>
    </Grid>
</Window>

I had to add the namespace WpfApp3. to x:Class, and I had to change x:Name not to be the same as x:Class. This also means I have to change all of the binding element names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    VB -> C#Specific to VB -> C# conversion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions