I derived from the System.Windows.Controls.ScrollViewer and changed the Template; my idea was to create a ScrollViewer as seen in Visual Studio 2010, which has a small ComboBox to set the zoom into the text.
The "ZoomPercentageValueComboBox" has the following DependencyProperty:
public double ZoomValue
{
get { return (double)GetValue(ZoomValueProperty); }
set { SetValue(ZoomValueProperty, value); }
}
public static readonly DependencyProperty ZoomValueProperty =
DependencyProperty.Register("ZoomValue", typeof(double),
typeof(ZoomPercentageComboBox), new FrameworkPropertyMetadata(1D, FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(ZoomValue_PropertyChanged)));
private static void ZoomValue_PropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs dpcea)
{
((ZoomPercentageComboBox)dpo).Text = (((double)dpcea.NewValue) * 100).ToString() + " %";
}
The new ScrollViewer has the following method:
private static ControlTemplate CreateDefaultControlTemplate()
{
#region DEFAULT
ControlTemplate template = null;
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Grid), "Grid");
FrameworkElementFactory child = new FrameworkElementFactory(typeof(ColumnDefinition), "ColumnDefinitionOne");
FrameworkElementFactory factory3 = new FrameworkElementFactory(typeof(ColumnDefinition), "ColumnDefinitionTwo");
FrameworkElementFactory factory4 = new FrameworkElementFactory(typeof(RowDefinition), "RowDefinitionOne");
FrameworkElementFactory factory5 = new FrameworkElementFactory(typeof(RowDefinition), "RowDefinitionTwo");
FrameworkElementFactory factory6 = new FrameworkElementFactory(typeof(ScrollBar), "PART_VerticalScrollBar");
FrameworkElementFactory factory7 = new FrameworkElementFactory(typeof(ScrollBar), "PART_HorizontalScrollBar");
FrameworkElementFactory factory8 = new FrameworkElementFactory(typeof(ScrollContentPresenter), "PART_ScrollContentPresenter");
FrameworkElementFactory factory9 = new FrameworkElementFactory(typeof(Rectangle), "Corner");
Binding binding = new Binding("HorizontalOffset")
{
Mode = BindingMode.OneWay,
RelativeSource = RelativeSource.TemplatedParent
};
Binding binding2 = new Binding("VerticalOffset")
{
Mode = BindingMode.OneWay,
RelativeSource = RelativeSource.TemplatedParent
};
factory.SetValue(Panel.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
factory.AppendChild(child);
factory.AppendChild(factory3);
factory.AppendChild(factory4);
factory.AppendChild(factory5);
factory.AppendChild(factory9);
factory.AppendChild(factory8);
factory.AppendChild(factory6);
factory.AppendChild(factory7);
child.SetValue(ColumnDefinition.WidthProperty, new GridLength(1.0, GridUnitType.Star));
factory3.SetValue(ColumnDefinition.WidthProperty, new GridLength(1.0, GridUnitType.Auto));
factory4.SetValue(RowDefinition.HeightProperty, new GridLength(1.0, GridUnitType.Star));
factory5.SetValue(RowDefinition.HeightProperty, new GridLength(1.0, GridUnitType.Auto));
factory8.SetValue(Grid.ColumnProperty, 0);
factory8.SetValue(Grid.RowProperty, 0);
factory8.SetValue(FrameworkElement.MarginProperty, new TemplateBindingExtension(Control.PaddingProperty));
factory8.SetValue(ContentControl.ContentProperty, new TemplateBindingExtension(ContentControl.ContentProperty));
factory8.SetValue(ContentControl.ContentTemplateProperty, new TemplateBindingExtension(ContentControl.ContentTemplateProperty));
factory8.SetValue(CanContentScrollProperty, new TemplateBindingExtension(CanContentScrollProperty));
factory7.SetValue(ScrollBar.OrientationProperty, Orientation.Horizontal);
factory7.SetValue(Grid.ColumnProperty, 0);
factory7.SetValue(Grid.RowProperty, 1);
factory7.SetValue(RangeBase.MinimumProperty, 0.0);
factory7.SetValue(RangeBase.MaximumProperty, new TemplateBindingExtension(ScrollableWidthProperty));
factory7.SetValue(ScrollBar.ViewportSizeProperty, new TemplateBindingExtension(ViewportWidthProperty));
factory7.SetBinding(RangeBase.ValueProperty, binding);
factory7.SetValue(UIElement.VisibilityProperty, new Te emplateBindingExtension(ComputedHorizontalScrollBarVisibilityProperty));
factory7.SetValue(FrameworkElement.CursorProperty, Cursors.Arrow);
factory7.SetValue(AutomationProperties.AutomationIdProperty, "HorizontalScrollBar");
factory6.SetValue(Grid.ColumnProperty, 1);
factory6.SetValue(Grid.RowProperty, 0);
factory6.SetValue(RangeBase.MinimumProperty, 0.0);
factory6.SetValue(RangeBase.MaximumProperty, new TemplateBindingExtension(ScrollableHeightProperty));
factory6.SetValue(ScrollBar.ViewportSizeProperty, new TemplateBindingExtension(ViewportHeightProperty));
factory6.SetBinding(RangeBase.ValueProperty, binding2);
factory6.SetValue(UIElement.VisibilityProperty, new TemplateBindingExtension(ComputedVerticalScrollBarVisibilityProperty));
factory6.SetValue(FrameworkElement.CursorProperty, Cursors.Arrow);
factory6.SetValue(AutomationProperties.AutomationIdProperty, "VerticalScrollBar");
factory9.SetValue(Grid.ColumnProperty, 1);
factory9.SetValue(Grid.RowProperty, 1);
factory9.SetResourceReference(Shape.FillProperty, SystemColors.ControlBrushKey);
#endregion
/* *** */
factory7.SetValue(MarginProperty, new Thickness(ZoomBoxWidth, 0, 0, 0));
/* *** */
FrameworkElementFactory factory10 = new FrameworkElementFactory(typeof(ZoomPercentageComboBox), ZoomBoxTemplateName);
factory.AppendChild(factory10);
factory10.SetValue(Grid.ColumnProperty, 0);
factory10.SetValue(Grid.RowProperty, 1);
factory10.SetValue(ZoomPercentageComboBox.HorizontalAlignmentProperty,
HorizontalAlignment.Left);
factory10.SetValue(ZoomPercentageComboBox.HeightProperty, 17D);
factory10.SetValue(ZoomPercentageComboBox.WidthProperty, ZoomBoxWidth);
factory10.SetValue(ZoomPercentageComboBox.IsEditableProperty, true);
factory10.SetValue(ZoomPercentageComboBox.VerticalContentAlignmentProperty, VerticalAlignment.Top);
factory10.SetValue(ZoomPercentageComboBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Left);
factory10.SetValue(ZoomPercentageComboBox.FontSizeProperty, 10D);
factory10.SetValue(ZoomPercentageComboBox.FontFamilyProperty, new FontFamily("Arial"));
factory10.SetValue(ZoomPercentageComboBox.BorderBrushProperty, null);
factory10.SetValue(ZoomPercentageComboBox.BorderThicknessProperty, new Thickness(0));
/* **************************************************************
???
*/
template = new ControlTemplate(typeof(LSArbeitscanvasScrollViewer))
{
VisualTree = factory
};
template.Seal();
return template;
}
THis is actually taken from the original ScrollViewer. Now I want to make shure that if the "ZoomValueProperty" from the ComboBox changes, the DependencyProperty "ZoomPercentageValueProperty", which is a class member of the ScrollViewer, changes, too. I had the idea to do it like this:
Binding binding = new Binding("HorizontalOffset")
{
Mode = BindingMode.OneWay,
RelativeSource = RelativeSource.TemplatedParent
};
but this didn't really work
I finally solved this by using a RoutedEvent in the class of the ComboBox and checking wheter the Source of the event is the templated ComboBox:
private void ZoomPercentageComboBox_ZoomValueChanged(object sender, ZoomValueChangedEventArgs e)
{
if (e.Source == templatedZoomPercentageComboBox)
ZoomPercentageComboBoxZoomPercentageValue = ((double)e.NewValue);
}
private const string ZoomBoxTemplateName = "PART_ZoomBox";
private ZoomPercentageComboBox templatedZoomPercentageComboBox
{
get
{
return this.GetTemplateChild(ZoomBoxTemplateName) as ZoomPercentageComboBox;
}
}
Related
I have in XAML a Window Resource Style targeted at TextBoxes, which style triggers and validations. These work great with my XAML TextBoxes. So far, so good.
What I'd like to do differs from this situation that the TextBoxes are created dynamically, not in XAML.
Please take special attention that there's a parameter to be passed to the ValidationRule, named WhatToCheck.
Here is my code so far:
private StackPanel NumberList(string queryLabel, string businessModelObjectName)
{
TestData currentTestData = dTestDataHolder["CurrentTestData"];
StackPanel spNumberList = new StackPanel();
spNumberList.Orientation = Orientation.Horizontal;
spNumberList.FlowDirection = System.Windows.FlowDirection.RightToLeft;
spNumberList.Margin = new Thickness(0, 0, 0, 10);
Label lNumberList = new Label();
lNumberList.Content = queryLabel;
lNumberList.FontSize = 16;
lNumberList.FontWeight = FontWeights.Bold;
spNumberList.Children.Add(lNumberList);
TextBox tbNumberList = new TextBox();
tbNumberList.Margin = new Thickness(10, 0, 0, 0);
tbNumberList.FontSize = 16;
tbNumberList.VerticalContentAlignment = VerticalAlignment.Center;
tbNumberList.FontFamily = new FontFamily("Courier New");
tbNumberList.Width = 100;
tbNumberList.AcceptsReturn = false;
tbNumberList.FlowDirection = FlowDirection.LeftToRight;
spNumberList.Children.Add(tbNumberList);
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);
return spNumberList;
}
public class GenericValidationRule : ValidationRule
{
public string WhatToCheck{ get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var enteredString = value as string;
switch (WhatToCheck)
{
case "No Extension":
{
if (enteredString.IndexOf(".doc", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
case "Only Numbers":
{
double dDummy;
if (!double.TryParse(enteredString, out dDummy))
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
default:
break;
}
return new ValidationResult(true, null);
}
}
Using c# and Visual Studio 2013.
No MVVM.
Thanks!
I ended up doing this. Worked great,
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
bindingNumberList.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bindingNumberList.ValidatesOnDataErrors = true;
GenericValidationRule gvrCheckValidImageNubers = new GenericValidationRule();
gvrCheckValidImageNubers.ValidationStep = ValidationStep.RawProposedValue;
gvrCheckValidImageNubers.WhatToCheck = "Contains Existing Image Numbers";
bindingNumberList.ValidationRules.Add(gvrCheckValidImageNubers);
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);
I am currently working on a WPF project that uses Caliburn.Micro and have hit a snag I was hoping you could help with. I have a form that allows the user to add new fields to a separate form with the intention that these new fields will persist. I am able to create these controls dynamically with the following code:
Grid tmpOuterGrid = new Grid();
RowDefinition rowDefinition1 = new RowDefinition();
RowDefinition rowDefinition2 = new RowDefinition();
rowDefinition1.Height = new GridLength(45, GridUnitType.Star);
rowDefinition2.Height = new GridLength(55, GridUnitType.Star);
tmpOuterGrid.RowDefinitions.Add(rowDefinition1);
tmpOuterGrid.RowDefinitions.Add(rowDefinition2);
tmpOuterGrid.Margin = new Thickness(0,0,0,10);
Grid tmpInnerGrid = new Grid();
Grid.SetRow(tmpInnerGrid, 0);
tmpInnerGrid.Margin = new Thickness(10,0,0,5);
tmpInnerGrid.Opacity = 0;
DataTrigger d = new DataTrigger();
Binding b = new Binding("DisplayFieldName");
b.Source = _fieldNames[primaryNameBase + "FieldTextBox"];
d.Binding = b;
d.Value = true;
Storyboard sbEnter = new Storyboard();
DoubleAnimation opAnimShow = new DoubleAnimation(1,new Duration(new TimeSpan(0,0,0,1)));
Storyboard.SetTargetProperty(sbEnter, new PropertyPath(UIElement.OpacityProperty));
sbEnter.Children.Add(opAnimShow);
Storyboard sbExit = new Storyboard();
DoubleAnimation opAnimHide = new DoubleAnimation(0, new Duration(new TimeSpan(0, 0, 0, 1)));
Storyboard.SetTargetProperty(sbExit, new PropertyPath(UIElement.OpacityProperty));
sbExit.Children.Add(opAnimHide);
BeginStoryboard bsEnter = new BeginStoryboard { Storyboard = sbEnter, Name = "beginStoryboardEnter" };
BeginStoryboard bsExit = new BeginStoryboard { Storyboard = sbExit, Name = "beginStoryboardExit" };
d.EnterActions.Add(bsEnter);
d.ExitActions.Add(bsExit);
Style st = new Style(typeof(Grid));
st.Triggers.Add(d);
tmpInnerGrid.Style = st;
TextBlock tb = new TextBlock();
tb.Name = primaryNameBase + "FieldHeaderText";
tb.Foreground = new SolidColorBrush(Color.FromArgb(136,255,255,255));
tb.FontSize = 14;
tb.Style = (Style) App.Current.Resources["NexaLightTextBlock"];
tb.HorizontalAlignment = HorizontalAlignment.Center;
tb.Text = fieldTextBox.DisplayText;
Border underline = new Border();
underline.BorderThickness = new Thickness(0, 0, 0, 1);
underline.BorderBrush = new SolidColorBrush(Color.FromArgb(136,49,250,250));
underline.VerticalAlignment = VerticalAlignment.Bottom;
underline.HorizontalAlignment = HorizontalAlignment.Center;
Binding binding = new Binding
{
Source = tb,
Path = new PropertyPath("ActualWidth"),
};
underline.SetBinding(FrameworkElement.WidthProperty, binding);
tmpInnerGrid.Children.Add(tb);
tmpInnerGrid.Children.Add(underline);
LowBorderTextBox lbtb = new LowBorderTextBox();
lbtb.Name = primaryNameBase + "FieldTextBox";
Grid.SetRow(lbtb, 1);
lbtb.Width = 140;
lbtb.Margin = new Thickness(10,0,0,0);
lbtb.FontColor = new SolidColorBrush(Colors.White);
lbtb.DisplayFontSize = 22;
lbtb.Style = (Style) App.Current.Resources["NexaLightCustomTextBox"];
lbtb.DisplayText = fieldTextBox.DisplayText;
lbtb.LostFocus += FieldLostFocus;
tmpOuterGrid.Children.Add(tmpInnerGrid);
tmpOuterGrid.Children.Add(lbtb);
wrapPanel.Children.Add(tmpOuterGrid);
I would then like these new controls to be persisted through application shutdowns. The approach I was thinking of taking was serializing the object into xaml and storing that in a file, and then reading this file and deserializing it to obtain the control object again, which would be added to my surrounding WrapPanel. This is all fine and dandy except for one detail. The controls that are created have a style with a datatrigger that is bound to a property of a NoteField object, there being one NoteField object for each of these types of control. I was planning on serializing the NoteField objects as well, so that I could just pull them back and hope that the binding would still be intact, but they implement the PropertyChangedBase interface of the Caliburn.Micro framework, and the NotifyOfPropertyChange() method is not marked as serializable. Here is the NoteField class:
[Serializable]
class NoteField : PropertyChangedBase
{
private string _controlName;
private bool _displayFieldName;
public bool DisplayFieldName
{
get { return _displayFieldName; }
set
{
_displayFieldName = value;
NotifyOfPropertyChange(() => DisplayFieldName);
}
}
private string _fieldName;
public string FieldName
{
get { return _fieldName; }
set
{
_fieldName = value;
NotifyOfPropertyChange(() => FieldName);
}
}
public NoteField(string controlName, string displayText)
{
DisplayFieldName = false;
_controlName = controlName;
FieldName = displayText;
}
public string GetControlName()
{
return _controlName;
}
public void SetControlName(string name)
{
_controlName = name;
}
}
I am not attached to using this class, but it was the only way I could think of having dynamically generated properties to bind to from the xaml. So, I guess my question is: is there a way that I can dynamically create controls that have bindings in them that can persist through application shutdowns? Am I on the right track, or should I do something else all together? Any help would be greatly appreciated. Thank you so much for your time.
I am creating the binding in the code behind.
IEnumerable<IDictionary<string, object>> rows = dg1.ItemsSource.OfType<IDictionary<string, object>>();
IEnumerable<string> columns = rows.SelectMany(d => d.Keys). Distinct(StringComparer.OrdinalIgnoreCase);
foreach (string column in columns)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = column;
// Create a factory. This will create the controls in each cell of this
// column as needed.
FrameworkElementFactory factory =
new FrameworkElementFactory(typeof(TextBox));
**Binding b = new Binding() { Path=new PropertyPath(column)};**
b.Mode = BindingMode.TwoWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
// b.StringFormat = "d";
b.Converter = new MyConverter();
b.ConverterParameter = dg1;
factory.SetValue(TextBox.TextProperty, b);
// factory.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
// Create the template itself, and add the factory to it.
DataTemplate cellEditingTemplate = new DataTemplate();
cellEditingTemplate.VisualTree = factory;
col.CellTemplate = cellEditingTemplate;
dg1.Columns.Add(col);
Now, if the value of the column starts with an opening ( and if it does not contain the closing braces ) then it gives the following error..
Syntax error in PropertyPath 'Unmatched parenthesis'
You can simply reproduce it like this:
public Window3()
{
try
{
InitializeComponent();
Binding b = new Binding() { Path = new PropertyPath("a(") };
}
catch (Exception)
{
throw;
}
}
Moreover if the PropertyPath has the value like (abc) then also the binding fails .
What could be the solution to avoid such binding issues?
EDIT:
Any hint from this answer.
I am trying to create a generic view, and I want it to contain a ListBox with a datatemplate
I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.
What I have made until now is
private static ListBox CreateDayListBox()
{
var listBox = new ListBox();
var dataTemplate = new DataTemplate();
var grid = new Grid();
var columnDefinition1 = new ColumnDefinition {Width = GridLength.Auto};
var columnDefinition2 = new ColumnDefinition();
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
var rectangleItemBought = new Rectangle {Width = 50, Height = 50};
rectangleItemBought.SetBinding(Rectangle.FillProperty, new Binding("Bought"));
grid.Children.Add(rectangleItemBought);
var textBlockItemName = new TextBlock();
textBlockItemName.SetBinding(TextBlock.TextProperty, new Binding("Name"));
var textBlockItemQuantity = new TextBlock();
textBlockItemQuantity.SetBinding(TextBlock.TextProperty, new Binding("Quantity"));
var textBlockItemQuantityType = new TextBlock();
textBlockItemQuantityType.SetBinding(TextBlock.TextProperty, new Binding("QuantityType"));
var stackpanel = new StackPanel();
Grid.SetColumn(stackpanel, 1);
stackpanel.Children.Add(textBlockItemName);
stackpanel.Children.Add(textBlockItemQuantity);
stackpanel.Children.Add(textBlockItemQuantityType);
grid.Children.Add(stackpanel);
return listBox;
}
So I want the listbox datatemplate to contain 1 rectangle, 1 stackpanel with 3 textboxes inside
You can write the template in XAML then load it in your code.
Read this.
Besides, I'm pretty sure you can create the DataTemplate by code the same way you created the controls, look at this code (credit):
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory =
new FrameworkElementFactory(typeof(StackPanel));
template.VisualTree = factory;
FrameworkElementFactory childFactory =
new FrameworkElementFactory(typeof(Image));
childFactory.SetBinding(Image.SourceProperty, new Binding("Machine.Thumbnail"));
childFactory.SetValue(Image.WidthProperty, 170.0);
childFactory.SetValue(Image.HeightProperty, 170.0);
factory.AppendChild(childFactory);
childFactory = new FrameworkElementFactory(typeof(Label));
childFactory.SetBinding(Label.ContentProperty,
new Binding("Machine.Descriiption"));
childFactory.SetValue(Label.WidthProperty, 170.0);
childFactory.SetValue(Label.HorizontalAlignmentProperty,
HorizontalAlignment.Center);
factory.AppendChild(childFactory);
I have a dynamic Datagrid that I have created. I am creating each column for it through code behind. I am having troubles on a column that I want to be displayed at a textblock when not editing, but as a combobox while editing. I have an ObservableCollection of Transactions. Each Transaction has a type called "Account". Here is what I have so far:
private DataGridTemplateColumn GetAccountColumn()
{
// Create The Column
DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
accountColumn.Header = "Account";
Binding bind = new Binding("Account");
bind.Mode = BindingMode.TwoWay;
// Create the TextBlock
FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
textFactory.SetBinding(TextBlock.TextProperty, bind);
DataTemplate textTemplate = new DataTemplate();
textTemplate.VisualTree = textFactory;
// Create the ComboBox
bind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
comboFactory.SetValue(ComboBox.DataContextProperty, this.Transactions);
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
comboFactory.SetBinding(ComboBox.ItemsSourceProperty, bind);
DataTemplate comboTemplate = new DataTemplate();
comboTemplate.VisualTree = comboFactory;
// Set the Templates to the Column
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
return accountColumn;
}
The value displays in the TextBlock. However, in the combobox, I am only getting one character to display per item. For example, here is the textblock:
But when I click to edit and go into the combobox, here is what is shown:
Can someone help me out so that the items in the Combobox are displayed properly? Also, when I select something from the Combobox, the textblock isn't updated with the item I selected.
UPDATED:
Here is my column as of now. The items in the ComboBox are being displayed properly. The issue now is that when a new item is selected, the text in the TextBlock isn't updated with the new item.
private DataGridTemplateColumn GetAccountColumn()
{
// Create The Column
DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
accountColumn.Header = "Account";
Binding bind = new Binding("Account");
bind.Mode = BindingMode.OneWay;
// Create the TextBlock
FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
textFactory.SetBinding(TextBlock.TextProperty, bind);
DataTemplate textTemplate = new DataTemplate();
textTemplate.VisualTree = textFactory;
// Create the ComboBox
Binding comboBind = new Binding("Account");
comboBind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
comboFactory.SetValue(ComboBox.ItemsSourceProperty, this.Accounts);
comboFactory.SetBinding(ComboBox.SelectedItemProperty, comboBind);
DataTemplate comboTemplate = new DataTemplate();
comboTemplate.VisualTree = comboFactory;
// Set the Templates to the Column
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
return accountColumn;
}
The "Accounts" property is declared like this in my MainWindow class:
public ObservableCollection<string> Accounts { get; set; }
public MainWindow()
{
this.Types = new ObservableCollection<string>();
this.Parents = new ObservableCollection<string>();
this.Transactions = new ObservableCollection<Transaction>();
this.Accounts = new ObservableCollection<string>();
OpenDatabase();
InitializeComponent();
}
Here is my Transaction Class:
public class Transaction
{
private string date;
private string number;
private string account;
public string Date
{
get { return date; }
set { date = value; }
}
public string Number
{
get { return number; }
set { number = value; }
}
public string Account
{
get { return account; }
set { account = value; }
}
}
You bind the ItemsSource to the selected value, a string, aka char array, so every character is used as an item, the ItemsSource binding presumably should target some other collection from which the value can be chosen.
Dim newBind As Binding = New Binding("LinktoCommonOutputBus")
newBind.Mode = BindingMode.OneWay
factory1.SetValue(ComboBox.ItemsSourceProperty, dictionary)
factory1.SetValue(ComboBox.NameProperty, name)
factory1.SetValue(ComboBox.SelectedValuePathProperty, "Key")
factory1.SetValue(ComboBox.DisplayMemberPathProperty, "Value")
factory1.SetBinding(ComboBox.SelectedValueProperty, newBind)
By creating Binding you can set SelectedValue in a datagrid for WPF.