WPF Xaml转换器错误

时间:2012-11-27 18:08:22

标签: wpf

我收到编译错误

  

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

下面是我的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>

1 个答案:

答案 0 :(得分: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中找到。