C# 完成转换器和任务';t存在于名称空间中
所以我试图从msdn中创建datagrid 但我得到了一个错误,我不知道为什么,因为我复制粘贴整个事情直接从msdn 我在屏幕上看到了错误C# 完成转换器和任务';t存在于名称空间中,c#,wpf,C#,Wpf,所以我试图从msdn中创建datagrid 但我得到了一个错误,我不知道为什么,因为我复制粘贴整个事情直接从msdn 我在屏幕上看到了错误 <local:CompleteConverter x:Key="completeConverter" /> <local:Tasks x:Key="tasks" /> 就是说 它不存在于clr命名空间中:DGGroupSortFilterExample CS 命名空间DGGroupSortFilterExample {
<local:CompleteConverter x:Key="completeConverter" />
<local:Tasks x:Key="tasks" />
就是说
它不存在于clr命名空间中:DGGroupSortFilterExample
CS
命名空间DGGroupSortFilterExample
{
公共类CompleteConverter:IValueConverter
{
//此转换器将任务完成状态的值从真/假更改为字符串值
//行组标题中使用的“完成”/“活动”。
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
bool complete=(bool)值;
如果(完成)
返回“完成”;
其他的
返回“活动”;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
字符串strComplete=(字符串)值;
如果(strComplete==“Complete”)
返回true;
其他的
返回false;
}
}
公共类任务:INotifyPropertyChanged,IEditableObject
{
//任务类实现INotifyPropertyChanged和IEditableObject
//以便datagrid能够正确响应对
//在DataGrid中进行的数据收集和编辑。
//私有任务数据。
私有字符串m_ProjectName=string.Empty;
私有字符串m_TaskName=string.Empty;
private DateTime m_DueDate=DateTime.Now;
private bool m_Complete=假;
//用于撤消已取消编辑的数据。
私有任务临时任务=null;
私有布尔m_编辑=假;
//公共财产。
公共字符串项目名
{
获取{返回this.m_ProjectName;}
设置
{
if(值!=此.m_项目名称)
{
this.m_ProjectName=值;
NotifyPropertyChanged(“项目名称”);
}
}
}
公共类任务:ObservableCollection
{
//以这种方式创建Tasks集合可以从XAML进行数据绑定。
}
}
Xaml
首先,尝试编译。只有在编译之后,设计师才会知道新的类。如果在编译之后没有解决这个问题,请发布整个XAML文档,以及该类的c#代码。确保名称空间和using语句可见。并且您是否包含示例中的CompleteConverter
类?是否为在DGGroupSortFilterExample
名称空间中?是的,但应该是一个外部类。对不起,我完全不理解这个评论。请在问题中添加一个WPF/SL标记,伙计。@jon skeet你知道的,右键单击你的csproj>add>类,这就是我所说的外部类。请同时发布完整的XAML文档。另外,电话:例如,转换器和XAML文档是否在同一个VS项目中?请注意“本地”xmlns是为XAML文档所在项目的根命名空间自动生成的xmlns。因此,设计师没有找到转换器的另一个原因是,本地xmlns确实不包含转换器,因为它位于另一个项目中。是的,我只是将它直接从msdn复制粘贴到XAML和CS中这在他们的文件上
namespace DGGroupSortFilterExample
{
public class CompleteConverter : IValueConverter
{
// This converter changes the value of a Tasks Complete status from true/false to a string value of
// "Complete"/"Active" for use in the row group header.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool complete = (bool)value;
if (complete)
return "Complete";
else
return "Active";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strComplete = (string)value;
if (strComplete == "Complete")
return true;
else
return false;
}
}
public class Task : INotifyPropertyChanged, IEditableObject
{
// The Task class implements INotifyPropertyChanged and IEditableObject
// so that the datagrid can properly respond to changes to the
// data collection and edits made in the DataGrid.
// Private task data.
private string m_ProjectName = string.Empty;
private string m_TaskName = string.Empty;
private DateTime m_DueDate = DateTime.Now;
private bool m_Complete = false;
// Data for undoing canceled edits.
private Task temp_Task = null;
private bool m_Editing = false;
// Public properties.
public string ProjectName
{
get { return this.m_ProjectName; }
set
{
if (value != this.m_ProjectName)
{
this.m_ProjectName = value;
NotifyPropertyChanged("ProjectName");
}
}
}
public class Tasks : ObservableCollection<Task>
{
// Creating the Tasks collection in this way enables data binding from XAML.
}
}
<Window x:Class="DGGroupSortFilterExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DGGroupSortFilterExample"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="Group, Sort, and Filter Example" Height="575" Width="525">
<Window.Resources>
<local:CompleteConverter x:Key="completeConverter" />
<local:Tasks x:Key="tasks" />
<CollectionViewSource x:Key="cvsTasks" Source="{StaticResource tasks}"
Filter="CollectionViewSource_Filter">
<CollectionViewSource.SortDescriptions>
<!-- Requires 'xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"' declaration. -->
<scm:SortDescription PropertyName="ProjectName"/>
<scm:SortDescription PropertyName="Complete" />
<scm:SortDescription PropertyName="DueDate" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ProjectName"/>
<PropertyGroupDescription PropertyName="Complete"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<DataGrid x:Name="dataGrid1"
ItemsSource="{Binding Source={StaticResource cvsTasks}}"
CanUserAddRows="False">
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<!-- Style for groups under the top level. -->
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<DockPanel Background="LightBlue">
<TextBlock Text="{Binding Path=Name, Converter={StaticResource completeConverter}}" Foreground="Blue" Margin="30,0,0,0" Width="100"/>
<TextBlock Text="{Binding Path=ItemCount}" Foreground="Blue"/>
</DockPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="White" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBlock Text=" Filter completed items " VerticalAlignment="Center" />
<CheckBox x:Name="cbCompleteFilter" VerticalAlignment="Center"
Checked="CompleteFilter_Changed" Unchecked="CompleteFilter_Changed" />
<Button Content="Remove Groups" Margin="10,2,2,2" Click="UngroupButton_Click" />
<Button Content="Group by Project/Status" Margin="2" Click="GroupButton_Click" />
</StackPanel>
</Grid>
</Window>