Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法成功地将WPF控件绑定到C#变量。无法解析该资源_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

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

我无法成功地将WPF控件绑定到C#变量。无法解析该资源,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,声明: ItemsSource="{Binding Source={StaticResource TTColumn}, Path=Names}" 给出了错误: 无法解析资源列 整个.xaml代码是: <controls:MetroWindow x:Class="XLTT.Views.RemoveColumn" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

声明:

ItemsSource="{Binding Source={StaticResource TTColumn}, Path=Names}"
给出了错误:

无法解析资源列

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

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

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

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

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

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

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

如果要将控件绑定到静态属性,应该使用
x:static
而不是
StaticResource
属性(因为TTColumn不是静态资源)

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


如果要将控件绑定到静态属性,应使用
x:static
而不是
StaticResource
属性(因为TTColumn不是静态资源)

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


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