1

我收到一个编译错误

错误 1 ​​找不到类型或命名空间名称“转换器”(您是否缺少 using 指令或程序集引用?) G:\C#\Practice\DataGrid\DataGrid\obj\x86\Debug\MainWindow.g.cs 12 7 数据网格

下面是我的 Xaml 代码,我是 WPF 新手,请帮帮我。我有转换器类。

<Window  x:Class="DataGrid.MainWindow "
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="500" Width="700"
xmlns:c="clr-namespace:Converters">
<Window.Resources>
   <c:BoolToStringConverter x:Key="BoolToStringConverter" />
</Window.Resources>
    <Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="59*" />
        <RowDefinition Height="402*" />
    </Grid.RowDefinitions>
    <StackPanel Margin="0,55,0,0" Grid.RowSpan="2">
        <DataGrid ItemsSource="{Binding Path=Courses}" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="datagrid1" CanUserAddRows="False" HeadersVisibility="Column" RowDetailsVisibilityMode="Visible" VerticalScrollBarVisibility="Auto" CanUserSortColumns="True" CanUserResizeColumns="False" Height="339" Width="610">
            <DataGrid.Resources>
                <Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
                    <Setter Property="HorizontalAlignment" Value="Center" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="IsEnabled" Value="True" />
                    <Setter Property="Margin" Value="4" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Course Title" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Code}" VerticalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Course Description" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Descrption}" VerticalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Required" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding Path=IsRequired, Converter={StaticResource BoolToStringConverter}}" 
                                       VerticalAlignment="Center" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
    <StackPanel Margin="0,0,539,32">
        <TextBlock Text="Select Enroolment:" FontSize="15" 
                   Height="26"
                   Grid.RowSpan="1"
                   Width="134">
        </TextBlock>

    </StackPanel>
    <TextBlock DockPanel.Dock="Left"
               HorizontalAlignment="Left" 
               Text="Select Course:" 
               Width="139" FontSize="15" 
               Margin="0,32,0,0">
               </TextBlock>
    <ComboBox HorizontalAlignment="Right"
              Margin="0,0,69,33"
              Name="comboBox1"
              Width="476"
              Height="23"
              VerticalAlignment="Bottom" />
</Grid>

4

1 回答 1

1

xmlns:c="clr-namespace:Converters"

您应该检查您的 Converters 类的命名空间,这就是您应该放置的Converters. 如果Converters是你的班级,那么你应该把它所说的任何东西放在你的.cs文件中的命名空间之后。

例子:

 namepsace MyNamespace {
      public class Converters { }
 }

那么您的 XAML 将是:

 xmlns:c="clr-namespace:MyNamespace"

额外:如果您想更具体,也可以添加装配参数。

 xmlns:window="clr-namespace:MyNamespace;assembly=MyNamespace"

可以在 Build Properties 中找到 Assembly(右键单击 Project、Properties、Application、Assembly Information)

您的默认命名空间也可以在 Build Properties 中找到。

于 2012-11-27T18:11:17.500 回答