无法将WPF控件成功绑定到C#变量。资源无法解析

时间:2015-07-13 08:31:38

标签: c# wpf xaml data-binding

声明:

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

给出错误:

  

无法解析资源TTColumn。

整个.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: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>

以下是我尝试绑定到WPF的Names变量的定义。

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

2 个答案:

答案 0 :(得分:4)

如果要将控件绑定到静态属性,则应使用@include linear-gradient(hotpink, tomato); markup extension而不是x:Static(因为TTColumn不是静态资源)。

因此,您必须在MetroWindow xaml中添加对命名空间的引用:

StaticResource

然后用以下内容替换你的绑定:

<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"

我希望这可以帮到你。

答案 1 :(得分:1)

您需要为XLTT.Core.Models定义XAML命名空间,并在StaticResource扩展名中引用它。为此,我们需要知道声明命名空间的程序集名称 - ill将其称为SampleNamespace

首先将名称空间添加到MetroWindow元素。

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

现在引用绑定中的命名空间。

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