2 个关于选项卡和组合框的 WPF(Windows 演示基础)快速问题
我是一名非常新手的 WPF 程序员,因此在一些简单的任务中需要帮助。
1-如图所示,我想要 (+) 选项卡(它是现在使用 TabItem 创建的选项卡,还是应该是按钮或其他东西?:-/)来创建一个左侧的选项卡,例如 (A1),再次按下,应该创建(A2),有点像选项卡式浏览器......如果可能的话,按下(-)会删除用户提示中选定的选项卡。
替代文本 http://img191.imageshack.us/img191/4532/tabn.png< /a>
2-我想要,用户在文本框中输入文本,内容用逗号或分号分隔,并且该内容在按钮按下/单击时添加到 COMBOBOX,我猜,它需要某种数据绑定?不确定,..这样,到最后,文本框中的内容将成为组合框中的列表(按下按钮时)........................ 如果可能,在文本框中选择文本时,单击按钮(选择将文本框内容添加到组合框的相同或不同按钮),它会从文本框和组合框中删除相同的内容。
请帮忙,我正在学习 WPF,但需要紧急帮助,但以我目前的知识无法解决。谢谢。
I am a very novice WPF programmer, so need help in some simple tasks.
1- As the image shows, I want the (+) tab(it's a tab created with TabItem as of now or should it be button or something else? :-/) to create a tab left to it, say (A1), pressing again, should create (A2), sort of like in tabbed browsers......... And if possible, pressing (-) deletes the selected tab on user prompt.
alt text http://img191.imageshack.us/img191/4532/tabn.png
2- I want, that user enters text in the textbox, with content separated by comma, or semicolon and that content is added to a COMBOBOX on Button press/click, I guess, it requires some sort of Databinding? Not sure,.. Such that, by the end of it, content from textbox, becomes list within ComboBox (on Button press) ....................
And if possible, the text when selected in text box, on button click(same or different button which was chosen to add textbox content to combobox), it deletes the same from textbox and combobox .
Please help, I am learning WPF, but need this urgent help, which I cannot solve with my current knowledge of it. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据绑定和 MVVM 使一切变得更加容易。一开始比较困难,但最终会容易得多。
创建两个类,
Item
和ItemCollection
,它们都实现INotifyPropertyChanged
。Item
应公开字符串Text
属性,而ItemCollection
应公开ObservableCollection
Items< /code> 属性和
Item
SelectedItem
属性。让
ItemCollection
类的构造函数使用测试数据填充Items
并设置SelectedItem
。在您真正开始实现选项卡控件之前,这似乎需要做很多事情,但是相信我,您会喜欢结果的。 TabControl 的 XAML 将如下所示:
让我们了解它的作用。它创建一个
TabControl
。它创建一个ItemsCollection
对象并将其设置为TabControl
的DataContext
。您已将ItemSource
绑定到Items
,因此TabControl
将为每个项目创建一个TabItem
。它将把ItemContainerStyle
应用于每个TabItem
,从而将其Header
属性设置为Item
的文本
属性。当控件呈现选项卡的内容时,它会查找正在呈现的项目,搜索资源以查找
DataType
与该项目匹配的DataTemplate
,并使用该模板。由于我们在TabControl.Resources
中定义了一个,因此您会再次获得漂亮的蓝色背景和Text
属性。这似乎需要经历很多事情。但现在您不必编写任何操作 UI 的代码;您只需编写操作
ItemsCollection
的代码,UI 就会自行处理。现在让我们来添加新选项卡。我们要做的是将一个新项目添加到控件中,当它被选中时,将一个新项目添加到
Items
集合中。创建一个新类,名为
ControlItem
。让它从Item
派生。修改ItemsCollection
构造函数,使其添加的最后一项是ControlItem
,而不是Item
。并将该项目的Text
属性设置为“+”。将此方法添加到
ItemsCollection
:现在添加到窗口的代码隐藏,并作为
TabControl
的SelectionChanged
事件处理程序:您可以实现类似的从列表中删除项目的逻辑,但您需要向
ItemsCollection
引入另一个变量来跟踪先前选择的项目,以便您知道要删除哪个项目。您可以做的另一件事:在
Item
中实现一个Background
属性,并向绑定TabItem
的ItemContainerStyle
添加一个 setter > 的Background
属性。然后,您可以在ControlItem
中重载该属性,以便您的添加和删除选项卡看起来有所不同。您还可以为控件项实现不同的子类,并让它们公开您在
SelectionChanged
事件处理程序中调用的方法。这样,事件处理程序就不必知道所单击的控件项正在执行的操作。事实上,如果您将该方法作为Item
的一部分,并且让它不执行任何操作(除非它被重写),则窗口甚至不需要知道Item
子类。简而言之,这就是 MVVM 背后的哲学:将视图绑定到它几乎一无所知的对象。让视图模型对象控制发生的事情,以便视图不必这样做。
Everything's easier with data binding and MVVM. Harder at first, but ultimately lots easier.
Make two classes,
Item
andItemCollection
, that both implementINotifyPropertyChanged
.Item
should expose a stringText
property, whileItemCollection
should expose anObservableCollection<Item>
Items
property and anItem
SelectedItem
property.Make the
ItemCollection
class's constructor populateItems
with test data and setSelectedItem
.This seems like a lot to do before you actually get around to implementing your tab control, but trust me, you'll like the result. Your XAML for the TabControl will look something like this:
Let's understand what this does. It creates a
TabControl
. It creates anItemsCollection
object and sets it as theTabControl
'sDataContext
. You've boundItemSource
toItems
, so theTabControl
will create aTabItem
for each item. It will apply theItemContainerStyle
to eachTabItem
, which sets itsHeader
property to theItem
'sText
property.When the control renders the content of a tab, it finds the item it's rendering, searches through the resources to find a
DataTemplate
whoseDataType
matches the item, and uses that template. Since we've defined one inTabControl.Resources
, you get a nice blue background and theText
property again.This seems like a lot to go through. But now you don't have to write any code that manipulates the UI; you just write code that manipulates your
ItemsCollection
, and the UI pretty much takes care of itself.Now let's take care of adding new tabs. What we're going to do is add a new item to the control that, when it becomes selected, adds a new item to the
Items
collection.Create a new class, called, oh,
ControlItem
. Have it derive fromItem
. Modify yourItemsCollection
constructor so that the last item it adds is aControlItem
, not anItem
. And have it set theText
property on that item to "+".Add this method to
ItemsCollection
:Now add to your window's code-behind, and as the
SelectionChanged
event handler for yourTabControl
:You can implement similar logic to remove an item from the list, but you'll need to introduce another variable to
ItemsCollection
to track the previously selected item, so that you can know which item to delete.Another thing you can do: implement a
Background
property inItem
and add a setter to theItemContainerStyle
that binds theTabItem
'sBackground
property to it. Then you can overload that property inControlItem
, so that your add and delete tabs look different.You can also implement different subclasses for your control items, and have them expose a method that you call in the
SelectionChanged
event handler. That way, the event handler doesn't have to know anything about what the control item being clicked is doing. In fact, if you make the method part ofItem
, and have it do nothing unless it's overriden, the window doesn't even need to know thatItem
has subclasses.That's the philosophy behind MVVM in a nutshell: Bind the view to objects about which it knows almost nothing. Let the view-model objects control what happens, so that the view doesn't have to.
选项卡控件应该很简单。处理 + 选项卡上的 Click 事件,创建一个新选项卡并将其设为活动选项卡。应该很容易...我必须运行,所以现在无法向您显示代码答案。尝试一下,如有任何问题,请回来。
下一部分我可以提供更多帮助,
代码隐藏窗口(或您的类)必须实现 INotifyPropertyChanged。还将窗口的 DataContext 属性设置为其自身(例如,在 ctor 中),如
this.DataContext = this;
文本更改事件处理程序修改代码隐藏上的 Options 属性,并触发以下通知:属性已被修改。组合框会收到通知,因为它是绑定到该属性的数据并且会自我更新。
The tab control should be easy. Handle a Click event on the + tab, create a new tab and make it the active tab. Should be easy... I've gotta run so can't show you the code answer right now. Give it a try and get back with any issues.
The next part I can be a bit more helpful
the code-behind window (or your class) has to implement INotifyPropertyChanged. Also set the DataContext property of the window to itself (e.g. in the ctor ) like
this.DataContext = this;
The text changed event handler modifies the Options property on the code-behind and fires the notification that the property has been modified. The Combobox is notified since it is data bound to that property and self-updates.