user1283776
user1283776

Reputation: 21824

Am not able to successfully bind WPF control to C# variable. The resource could not be resolved

The statement:

ItemsSource="{Binding Source={StaticResource TTColumn}, Path=Names}"

gives the error:

The resource TTColumn could not be resolved.

The entire .xaml code is:

<controls:MetroWindow x:Class="XLTT.Views.RemoveColumn"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
                        GlowBrush="{DynamicResource AccentColorBrush}"
             ShowTitleBar="False"
             WindowStartupLocation="CenterOwner" 
             ResizeMode="NoResize" WindowStyle="ToolWindow"
              Height="320" Width="350"   Title="Remove Table Column">
    <controls:MetroWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </controls:MetroWindow.Resources>
    <Grid Margin="20 10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListBox Margin="5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource TTColumn}, Path=Names}" SelectedItem="{Binding SelectedColumn}" Grid.Row="1" BorderBrush="#FFF1F1F1" Background="#FFFFF9F9" Grid.ColumnSpan="3"/>

        <TextBlock HorizontalAlignment="Stretch" Margin="5 0 5 0" TextWrapping="Wrap" Text="Select Column:" VerticalAlignment="Top" Grid.ColumnSpan="3" FontWeight="Bold" FontFamily="Segoe UI Light" FontSize="24"/>


        <Button Name="btnOk" Height="30" Content="Remove" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Bottom" Width="68" Command="{Binding RemoveColumnCommand}" Grid.Row="2" Grid.Column="1"/>
        <Button Name="btnCancel" Height="30" Content="Close" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Bottom" Width="68" RenderTransformOrigin="1.867,0.75" Click="btnCancel_Click" Grid.Row="2" Grid.Column="2"/>

    </Grid>
</controls:MetroWindow>

Here is the definition of the Names variable that I am trying to bind to WPF.

namespace XLTT.Core.Models
{
    public class TTColumn
    {
        private static string[] names = {"one", "two", "three"};   // the name field
        public static string[] Names   // the Name property
        {
            get 
            {
                return names; 
            }
            set 
            {
                names = value;
            }
        }
    }
}

Upvotes: 0

Views: 320

Answers (2)

Il Vic
Il Vic

Reputation: 5666

If you want to bind a control to a static property you shoud use the x:Static markup extension instead of the StaticResource one (since TTColumn is not a static resource).

So you have to add a reference to the namespace in your MetroWindow xaml:

<controls:MetroWindow x:Class="XLTT.Views.RemoveColumn"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             ...
             xmlns:models="clr-namespace:XLTT.Core.Models"

then replace your binding with:

ItemsSource="{Binding Source={x:Static models:TTColumn.Names}}"

I hope this can help you.

Upvotes: 4

Gusdor
Gusdor

Reputation: 14322

You need to define an XAML namespace for XLTT.Core.Models and reference it in the StaticResource extension. For this, we need to know the assembly name that the namespace is declared in - ill refer to this as SampleNamespace

First add the namespace to the MetroWindow element.

<controls:MetroWindow x:Class="XLTT.Views.RemoveColumn"
             xmlns:models="clr-namespace:XLTT.Core.Models;assembly=SampleNamespace"

Now reference the namespace in the binding.

ItemsSource="{Binding Source={StaticResource models:TTColumn}, Path=Names}"

Upvotes: 1

Related Questions