'MyUserControls.ComboBox1' 不包含 'ItemsSource' 的定义
'MyUserControls.ComboBox1' does not contain definition for 'ItemsSource'
我在 MS 的 Blend 中创建了一个用户控件库,用于 WPF C# 项目。这是我第一次尝试使用 Blend 创建用户控件,但我在实现它时遇到了问题。
我的问题是 ComboBox
。当我转到代码隐藏时,我无法访问或设置 ComboBox1.ItemsSource
或任何其他特定属性。
以防万一是我的 ComboBox 导致了问题,我创建了一个 Chrome 模板 ComboBox
,并尝试了它,但我得到了相同的结果。
最终目标:这是一个功能ComboBox
我是否需要添加引用,以另一种方式访问 ItemsPresenter
,或者我应该做些什么来使它成为 'fully-functional' ComboBox
,比如应用我的模板到标准 ComboBox
还是应该按原样工作?
我已经对此进行了研究,但在创建 ComboBox
(this,this and this). I found this answer 之后找不到任何东西,但我不确定它是否相关,但它对我不起作用。
"To instantiate the user control in another document" looked promising, but didn't help. Neither did this.
MainWindow.xaml
(MyComboBox 是一个标准 'Chrome' ComboBox
放置在 Blend 的用户控件中)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyUserControls="clr-namespace:MyUserControls;assembly=MyUserControls"
x:Name="Window" x:Class="UserControlTester.MainWindow"
Title="MainWindow" Height="252.584" Width="557">
<Grid >
<MyUserControls:MyComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="324,201.713,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
测试程序MainWindow.xaml.cs
:
using System.Collections.Generic;
using System.Windows;
namespace UserControlTester
{
public partial class MainWindow : Window
{
private List<string> items;
public MainWindow()
{
InitializeComponent();
//add stuff to drives...
ComboBox1.ItemsSource = items; //Error on ItemsSource "'MyUserControls.ComboBox1' does not contain a definition for 'ItemsSource'..."
}
}
}
也许你的 MyComboBox
实际上不是 ComboBox
。
可能的解决方案:
public class MyComboBox : ComboBox { ... }
这种方式很难扩展xaml。
其他:
public class MyComboBox : UserControl {
public ItemsSource ItemsSource {
get { return this.ComboBox1.ItemsSource; }
set { this.ComboBox1.ItemsSource = value; }
}
}
不可扩展但简单明了(不打算在 xaml 中使用)。使用 DependencyProperty
用于 xaml-使用。
仍然,简单地说:
this.ComboBox1.InnterComboBox.ItemsSource = ...
您混淆了 UserControl
和 自定义控件 的概念,它们在 WPF 中是两个完全不同的概念,必须应用于不同的用例。我看到很多人都犯了这个错误,所以我会解释一下:
一个UserControl
是从System.Windows.Controls.UserControl
派生出来的class,表示由其他控件组合而成的"widget"。典型用例是作为特定模型或视图模型的 "view",例如 PersonDataView
UserControl,它包含 Person 对象的 LastName、FirstName 和 Age 属性的文本框和标签。 UserControl
s 通常由 XAML 文件 + 代码隐藏文件定义。代码和XAML耦合在一起,代码会对XAML.
声明的元素进行操作
一个自定义控件是一个class继承自任何WPF控件class(例如ComboBox
, TextBox
,等等,甚至 Control
,FrameworkElement
或 UIElement
)并添加 功能 (与修改或添加视觉外观相反,必须通过 Styling and Templating) 完成。典型的用例恰恰是向现有控件添加功能(例如,向 TickBar
添加数字),或者创建一个完全自定义的可视元素,该元素无法通过组合或自定义现有控件来实现(例如 LookupBox,它允许基于键盘搜索列表中的项目)。自定义控件通常由仅代码 class(无 XAML)定义,然后它的可视化树由 ControlTemplate
定义并通过 Style
分配给控件.该代码与 XAML 分离,如果您需要访问 ControlTemplate
.
中定义的元素,通常您将使用 Template.FindName()
在你的情况下,我假设你只是想改变现有的 ComboBox
的 视觉外观 ,因此我会简单地创建一个 ControlTemplate
ComboBox,它根本不需要自定义控件。
我强烈建议您阅读 MSDN 上的 Control Authoring Overview 以获取更多信息。
我在 MS 的 Blend 中创建了一个用户控件库,用于 WPF C# 项目。这是我第一次尝试使用 Blend 创建用户控件,但我在实现它时遇到了问题。
我的问题是 ComboBox
。当我转到代码隐藏时,我无法访问或设置 ComboBox1.ItemsSource
或任何其他特定属性。
以防万一是我的 ComboBox 导致了问题,我创建了一个 Chrome 模板 ComboBox
,并尝试了它,但我得到了相同的结果。
最终目标:这是一个功能ComboBox
我是否需要添加引用,以另一种方式访问 ItemsPresenter
,或者我应该做些什么来使它成为 'fully-functional' ComboBox
,比如应用我的模板到标准 ComboBox
还是应该按原样工作?
我已经对此进行了研究,但在创建 ComboBox
(this,this and this). I found this answer 之后找不到任何东西,但我不确定它是否相关,但它对我不起作用。
"To instantiate the user control in another document" looked promising, but didn't help. Neither did this.
MainWindow.xaml
(MyComboBox 是一个标准 'Chrome' ComboBox
放置在 Blend 的用户控件中)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyUserControls="clr-namespace:MyUserControls;assembly=MyUserControls"
x:Name="Window" x:Class="UserControlTester.MainWindow"
Title="MainWindow" Height="252.584" Width="557">
<Grid >
<MyUserControls:MyComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="324,201.713,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
测试程序MainWindow.xaml.cs
:
using System.Collections.Generic;
using System.Windows;
namespace UserControlTester
{
public partial class MainWindow : Window
{
private List<string> items;
public MainWindow()
{
InitializeComponent();
//add stuff to drives...
ComboBox1.ItemsSource = items; //Error on ItemsSource "'MyUserControls.ComboBox1' does not contain a definition for 'ItemsSource'..."
}
}
}
也许你的 MyComboBox
实际上不是 ComboBox
。
可能的解决方案:
public class MyComboBox : ComboBox { ... }
这种方式很难扩展xaml。
其他:
public class MyComboBox : UserControl {
public ItemsSource ItemsSource {
get { return this.ComboBox1.ItemsSource; }
set { this.ComboBox1.ItemsSource = value; }
}
}
不可扩展但简单明了(不打算在 xaml 中使用)。使用 DependencyProperty
用于 xaml-使用。
仍然,简单地说:
this.ComboBox1.InnterComboBox.ItemsSource = ...
您混淆了 UserControl
和 自定义控件 的概念,它们在 WPF 中是两个完全不同的概念,必须应用于不同的用例。我看到很多人都犯了这个错误,所以我会解释一下:
一个
UserControl
是从System.Windows.Controls.UserControl
派生出来的class,表示由其他控件组合而成的"widget"。典型用例是作为特定模型或视图模型的 "view",例如PersonDataView
UserControl,它包含 Person 对象的 LastName、FirstName 和 Age 属性的文本框和标签。UserControl
s 通常由 XAML 文件 + 代码隐藏文件定义。代码和XAML耦合在一起,代码会对XAML. 声明的元素进行操作
一个自定义控件是一个class继承自任何WPF控件class(例如
ComboBox
,TextBox
,等等,甚至Control
,FrameworkElement
或UIElement
)并添加 功能 (与修改或添加视觉外观相反,必须通过 Styling and Templating) 完成。典型的用例恰恰是向现有控件添加功能(例如,向TickBar
添加数字),或者创建一个完全自定义的可视元素,该元素无法通过组合或自定义现有控件来实现(例如 LookupBox,它允许基于键盘搜索列表中的项目)。自定义控件通常由仅代码 class(无 XAML)定义,然后它的可视化树由ControlTemplate
定义并通过Style
分配给控件.该代码与 XAML 分离,如果您需要访问ControlTemplate
. 中定义的元素,通常您将使用
Template.FindName()
在你的情况下,我假设你只是想改变现有的 ComboBox
的 视觉外观 ,因此我会简单地创建一个 ControlTemplate
ComboBox,它根本不需要自定义控件。
我强烈建议您阅读 MSDN 上的 Control Authoring Overview 以获取更多信息。