Todd710
asked on
How do I bind a WPF ComboBox to an enumerator in another class?
How do I bind a WPF ComboBox to an enumerator in another class?
I am using a VS2012 and making a WPF application. I am currently working through the details. I am trying to Databind:
This to a ComboBox and I am unable to create a CollectionViewSource for the enumerator. I am not even sure that is the way to go. Here is the rest of my current code:
MainWindow.xaml
Code Behind:
I am not sure I am headed in the right direction. Any guidance would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Diagnostics;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
using System.IO;
using System.Data;
using log4net;
namespace WPFLabelDatabindUpdateSample
{
public class ChronDatabasesList : ObservableCollection<ChronDatabases>
{
public ChronDatabasesList()
: base()
{
Add(new ChronDatabases());
}
}
public class ChronDatabases : INotifyPropertyChanged
{
public enum TargetDB { Primary, Secondary, Archive }
private string _PrimaryDBPath = string.Empty;
public string PrimaryDBPath
{
get { return _PrimaryDBPath; }
set
{
if (_PrimaryDBPath != value)
{
Utils.SetRegistryValue("SOFTWARE\\Chronicle\\Databases", "Primary", value);
_PrimaryDBPath = value;
OnPropertyChanged("PrimaryDBPath");
}
}
}
public enum ServerUpdateGroup
{
alpha = 1,
beta = 2,
beta2 = 3,
cmobile = 4,
cweb = 5,
cwebmobile = 6,
test = 7,
triage = 8,
zone1 = 9,
zone2 = 10,
zone3 = 11,
zone4 = 12
}
private ServerUpdateGroup _UpdateGroup;
public ServerUpdateGroup UpdateGroup
{
get { return _UpdateGroup; }
set
{
//Utils.SetServerSettingValue("SMTP Authentication", value.BoolToYN());
Utils.SetRegistryValue("SOFTWARE\\Chronicle\\Databases", "UpdateZone", value);
_UpdateGroup = value;
}
}
public ChronDatabases()
{
try
{
_PrimaryDBPath = Utils.GetRegistryValue("SOFTWARE\\Chronicle\\Databases", "Primary", string.Empty).ToString();
_UpdateGroup = (ServerUpdateGroup)Enum.Parse(typeof(ServerUpdateGroup), Utils.GetRegistryValue("SOFTWARE\\Chronicle\\Databases", "UpdateZone", "Not Set").ToString(), true);
}
catch (Exception ex)
{
EventLog.WriteEntry("ChronBackups-Databases", ex.Message, EventLogEntryType.Error, 49);
}
}
// Create the OnPropertyChanged method to raise the event
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
I am using a VS2012 and making a WPF application. I am currently working through the details. I am trying to Databind:
public enum ServerUpdateGroup
{
alpha = 1,
beta = 2,
beta2 = 3,
cmobile = 4,
cweb = 5,
cwebmobile = 6,
test = 7,
triage = 8,
zone1 = 9,
zone2 = 10,
zone3 = 11,
zone4 = 12
}
This to a ComboBox and I am unable to create a CollectionViewSource for the enumerator. I am not even sure that is the way to go. Here is the rest of my current code:
MainWindow.xaml
<Window
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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WPFLabelDatabindUpdateSample"
mc:Ignorable="d" x:Class="WPFLabelDatabindUpdateSample.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="chronDatabasesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:ChronDatabases}, CreateList=True}"/>
<CollectionViewSource x:Key="serverUpdateGroupEnumViewSource" x:Shared="False">
<CollectionViewSource.Source>
<ObjectDataProvider
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:ServerUpdateGroup" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</CollectionViewSource.Source>
</CollectionViewSource>
</Window.Resources>
<Grid>
<StackPanel x:Name="grid1" Margin="10,10,10,0" VerticalAlignment="Top" Height="300">
<Button x:Name="btnChangeValue" Content="Change Value" HorizontalAlignment="Left" Margin="52,37,0,0" VerticalAlignment="Top" Width="99" Click="btnChangeValue_Click"/>
<Label Content="{Binding PrimaryDBPath, Source={StaticResource chronDatabasesViewSource}}" Margin="52,30,10,-51" />
<ComboBox
ItemsSource="{Binding ServerUpdateGroup, Source={StaticResource serverUpdateGroupEnumViewSource}}"
SelectedItem="{Binding UpdateGroup, Source={StaticResource chronDatabasesViewSource}}">
</ComboBox>
</StackPanel>
</Grid>
</Window>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFLabelDatabindUpdateSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ChronDatabasesList _cdl = new ChronDatabasesList();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource chronDatabasesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("chronDatabasesViewSource")));
// Load data by setting the CollectionViewSource.Source property:
chronDatabasesViewSource.Source = _cdl;
}
private void btnChangeValue_Click(object sender, RoutedEventArgs e)
{
var item = _cdl.FirstOrDefault();
item.PrimaryDBPath = "Hello Again!";
}
}
}
I am not sure I am headed in the right direction. Any guidance would be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER