DataType绑定到文件夹内的类 [英] DataType Bind to Class inside a Folder
问题描述
您好,
我想将Combobox的DataTemplate绑定到数据类型Part,它位于我的CSharp项目中的Compnents文件夹中。当我尝试使用点表示法绑定时,(即Components.CPUPart)会得到一个类型未找到错误以及 嵌套
类型不支持错误。解决方案是什么。
您好DSBD,
根据您的描述,您需要绑定子文件夹中的数据(Compnents)。 因为您没有提供相关代码,我不确定是什么原因造成的。你能否提供相关的代码,可以重现这个问题。
此外,这里有一个包含子文件夹的示例,它使用MVVM模式作为参考。
#Model& ViewModel
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Windows.Data;
namespace WpfApp1.Compnents
{
public class PhoneBookEntry
{
public string CPUPart {get;组; }
public PhoneBookEntry(string name)
{
CPUPart = name;
}
公共覆盖字符串ToString()
{
返回CPUPart;
}
}
公共类ConnectionViewModel:INotifyPropertyChanged
{
public ConnectionViewModel()
{
IList< PhoneBookEntry> list = new List< PhoneBookEntry>();
list.Add(new PhoneBookEntry(" TEST1"));
list.Add(new PhoneBookEntry(" TEST2"));
list.Add(new PhoneBookEntry(" TEST3"));
list.Add(new PhoneBookEntry(" TEST4"));
list.Add(new PhoneBookEntry(" TEST5"));
_phonebookEntries = new CollectionView(list);
}
private readonly CollectionView _phonebookEntries;
private PhoneBookEntry _phonebookEntry;
public CollectionView EntitiesData
{
get {return _phonebookEntries; }
}
public PhoneBookEntry SelectedEntity
{
get {return _phonebookEntry; }
设置
{
if(_phonebookEntry == value)return;
_phonebookEntry = value;
OnPropertyChanged(" SelectedEntity");
}
}
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName的));
}
公共事件PropertyChangedEventHandler PropertyChanged;
}
}#XAML
< Window x:Class =" ; WpfApp1.Window22"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local =" clr-namespace:WpfApp1"
mc:Ignorable =" d"
Title =" Window22"高度= QUOT; 450"宽度= QUOT; 800">
< Grid>
< StackPanel>
< ComboBox Margin =" 20,50,20,20" X:名称= QUOT; combobox1"宽度= QUOT; 220" ItemsSource =" {Binding EntitiesData}" SelectedItem =" {Binding SelectedEntity}" >
< ComboBox.ItemTemplate>
< DataTemplate>
< Grid HorizontalAlignment =" Stretch">
< Grid.ColumnDefinitions>
< ColumnDefinition Width =" auto" />
< /Grid.ColumnDefinitions>
< TextBlock Text =" {Binding CPUPart,Mode = OneWay}"余量= QUOT; 5,0" />
< / Grid>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>
< / StackPanel>
< / Grid>
< / Window>#Code Behind
使用System.Windows;
名称空间WpfApp1
{
///< summary>
/// Window22.xaml的互动逻辑
///< / summary>
public partial class Window22:Window
{
public Window22()
{
InitializeComponent();
this.DataContext = new Compnents.ConnectionViewModel();
}
}
}祝你好运,
张龙
Hello,
I would like to bind the DataTemplate of a Combobox to the datatype Part, which resides in my Compnents Folder in my CSharp project. When I attempt to bind using dot notation, (i.e. Components.CPUPart) is get a type not found error aswell as a Nested Types not supported error. What is the solution.
Hi DSBD,
According to your description, you want to bind data in the subfolder(Compnents). because you have not provide related code, I am not sure what cause the issue. could you please provide related code, which could reproduce the issue.
In addition, here is a sample with subfolder, which use MVVM pattern for your reference.
#Model & ViewModel
using System.Collections.Generic; using System.ComponentModel; using System.Windows.Data; namespace WpfApp1.Compnents { public class PhoneBookEntry { public string CPUPart { get; set; } public PhoneBookEntry(string name) { CPUPart = name; } public override string ToString() { return CPUPart; } } public class ConnectionViewModel : INotifyPropertyChanged { public ConnectionViewModel() { IList<PhoneBookEntry> list = new List<PhoneBookEntry>(); list.Add(new PhoneBookEntry("TEST1")); list.Add(new PhoneBookEntry("TEST2")); list.Add(new PhoneBookEntry("TEST3")); list.Add(new PhoneBookEntry("TEST4")); list.Add(new PhoneBookEntry("TEST5")); _phonebookEntries = new CollectionView(list); } private readonly CollectionView _phonebookEntries; private PhoneBookEntry _phonebookEntry; public CollectionView EntitiesData { get { return _phonebookEntries; } } public PhoneBookEntry SelectedEntity { get { return _phonebookEntry; } set { if (_phonebookEntry == value) return; _phonebookEntry = value; OnPropertyChanged("SelectedEntity"); } } private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; } }#XAML
<Window x:Class="WpfApp1.Window22" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="Window22" Height="450" Width="800"> <Grid> <StackPanel> <ComboBox Margin="20,50,20,20" x:Name="combobox1" Width="220" ItemsSource="{Binding EntitiesData}" SelectedItem="{Binding SelectedEntity}" > <ComboBox.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding CPUPart, Mode=OneWay}" Margin="5,0" /> </Grid> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel> </Grid> </Window>#Code Behind
using System.Windows; namespace WpfApp1 { /// <summary> /// Interaction logic for Window22.xaml /// </summary> public partial class Window22 : Window { public Window22() { InitializeComponent(); this.DataContext = new Compnents.ConnectionViewModel(); } } }Best regards,
Zhanglong
这篇关于DataType绑定到文件夹内的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!