5
// Eric Maupin <ermau@xamarin.com>
7
// Copyright (c) 2012 Xamarin, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
29
using System.Windows.Controls;
30
using System.Windows.Data;
32
using Xwt.WPFBackend.Utilities;
33
using WindowsSeparator = System.Windows.Controls.Separator;
34
using WindowsComboBox = System.Windows.Controls.ComboBox;
35
using WindowsOrientation = System.Windows.Controls.Orientation;
36
using WindowsComboBoxItem = System.Windows.Controls.ComboBoxItem;
38
namespace Xwt.WPFBackend
40
public class ComboBoxBackend
41
: WidgetBackend, IComboBoxBackend
43
private static readonly Style ContainerStyle;
44
//private static readonly DataTemplate DefaultTemplate;
46
static ComboBoxBackend()
48
var factory = new FrameworkElementFactory (typeof (WindowsSeparator));
49
factory.SetValue (FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
51
var sepTemplate = new ControlTemplate (typeof (ComboBoxItem));
52
sepTemplate.VisualTree = factory;
54
DataTrigger trigger = new DataTrigger();
55
trigger.Binding = new Binding (".[1]") { Converter = new TypeToStringConverter() };
56
trigger.Value = typeof(ItemSeparator).Name;
57
trigger.Setters.Add (new Setter (Control.TemplateProperty, sepTemplate));
58
trigger.Setters.Add (new Setter (UIElement.IsEnabledProperty, false));
60
ContainerStyle = new Style (typeof (ComboBoxItem));
61
ContainerStyle.Triggers.Add (trigger);
64
public ComboBoxBackend()
66
ComboBox = new ExComboBox();
67
ComboBox.DisplayMemberPath = ".[0]";
68
//ComboBox.ItemTemplate = DefaultTemplate;
69
ComboBox.ItemContainerStyle = ContainerStyle;
72
public void SetViews (CellViewCollection views)
74
ComboBox.DisplayMemberPath = null;
75
ComboBox.ItemTemplate = GetDataTemplate (views);
78
public void SetSource (IListDataSource source, IBackend sourceBackend)
80
var dataSource = sourceBackend as ListDataSource;
81
if (dataSource != null)
82
ComboBox.ItemsSource = dataSource;
84
ComboBox.ItemsSource = new ListSourceNotifyWrapper (source);
87
public int SelectedRow
89
get { return ComboBox.SelectedIndex; }
90
set { ComboBox.SelectedIndex = value; }
93
public override void EnableEvent (object eventId)
95
base.EnableEvent (eventId);
97
if (eventId is ComboBoxEvent) {
99
switch ((ComboBoxEvent)eventId) {
100
case ComboBoxEvent.SelectionChanged:
101
ComboBox.SelectionChanged += OnSelectionChanged;
107
public override void DisableEvent (object eventId)
109
base.DisableEvent (eventId);
111
if (eventId is ComboBoxEvent) {
113
switch ((ComboBoxEvent)eventId) {
114
case ComboBoxEvent.SelectionChanged:
115
ComboBox.SelectionChanged -= OnSelectionChanged;
121
protected ExComboBox ComboBox
123
get { return (ExComboBox) Widget; }
124
set { Widget = value; }
127
protected IComboBoxEventSink ComboBoxEventSink
129
get { return (IComboBoxEventSink) EventSink; }
132
private void OnSelectionChanged (object sender, SelectionChangedEventArgs e)
134
Xwt.Engine.Toolkit.Invoke (ComboBoxEventSink.OnSelectionChanged);
137
private DataTemplate GetDataTemplate (IList<CellView> views)
139
var template = new DataTemplate (typeof (object[]));
141
FrameworkElementFactory root;
142
if (views.Count > 1) {
143
FrameworkElementFactory spFactory = new FrameworkElementFactory (typeof (StackPanel));
144
spFactory.SetValue (StackPanel.OrientationProperty, WindowsOrientation.Horizontal);
146
foreach (var view in views) {
147
spFactory.AppendChild (CellUtil.CreateBoundCellRenderer (view));
152
root = CellUtil.CreateBoundCellRenderer (views [0]);
155
template.VisualTree = root;