Olá gente.
Estou com um problema aqui que já me bati para resolver e não consegui ainda.
Estou aprendendo WPF, e estou tentando vincular um Enum a um ComboBox já tentei de diversas maneiras e nada, Com o código que consegui fazer sem erros eu consegui apenas alterar e até gravar a informação mas não consigo exibir novamente a informação ao carregar.
Alguém pode me ajudar por favor.
Segue os códigos da solução.
O enum que quero vincular.
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Business.Enum
{
public enum ConfNomesBancoDados
{
[DescriptionAttribute(“PostgreSQL 8.5”)]
PostgreSQl,
[DescriptionAttribute(“Firebird 2.5”)]
Firebird,
[DescriptionAttribute(“MySql 3.0”)]
MySql
}
}
[/code]
O Dicionário base e também com a implementação de um converter (Mas to pensando em separar o converter para aumentar a legibilidade.)
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections;
using System.Windows.Data;
namespace ClienteWPF
{
public class DicionaryEnumBase : Dictionary<String,Object>,IValueConverter
{
public DicionaryEnumBase()
{
}
private Type EnumType { get; set; }
public void Load(Type EnumType)
{
this.EnumType = EnumType;
Array enumValues = Enum.GetValues(EnumType);
foreach(Object t in enumValues){
Add( GetDescription(t,EnumType), t);
}
}
private string GetDescription(object enumValue, Type enumType)
{
var descriptionAttribute = enumType
.GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;
return descriptionAttribute != null
? descriptionAttribute.Description
: enumValue.ToString();
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String key = GetDescription(value,EnumType);
return key;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
KeyValuePair<String, Object> o = (KeyValuePair<String, Object>)value;
foreach (Object en in Enum.GetValues(EnumType))
{
String key = o.Key;
if (GetDescription(en, EnumType).Equals(key))
{
return en;
}
}
}
catch (Exception ex)
{
return value;
}
return value;
}
}
}
[/code]
O XAML que define a apresentação.
[code]
<Page.Resources>
<dic:ConfNomesBancoDadosDic x:Key=“nomesDb”/>
</Page.Resources>
<Button.BindingGroup>
</Button.BindingGroup>
<ComboBox Name="cboBancoDados"
HorizontalAlignment=“Left” Margin=“112,10,0,0” VerticalAlignment=“Top” Width=“358"
SelectedItem=”{Binding Path=NOME_BANCO_DADOS,Converter={StaticResource ResourceKey=nomesDb}}"
DisplayMemberPath=“Key”
ItemsSource="{StaticResource ResourceKey=nomesDb}"
SelectedValuePath="Value"
/>
<Label Content="Porta:" HorizontalAlignment="Left" Margin="383,41,0,0" VerticalAlignment="Top"/>
<Label Content="Banco:" HorizontalAlignment="Left" Margin="13,73,0,0" VerticalAlignment="Top" RenderTransformOrigin="-4.111,-2"/>
<Label Content="Banco de dados:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
[/code]
E finalmente o code-behind da apresentação.
[code]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 ClienteWPF
{
///
/// Interaction logic for ConfiguracaoView.xaml
///
public partial class ConfiguracaoView : Page
{
public ConfiguracaoView()
{
InitializeComponent();
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
DataContext = ClienteWPF.Properties.Settings.Default;
}
}
}
[/code]
Eu estou postando após um dia inteiro de tentativas sem sucesso realmente estou sem ideias e nem material para consulta. Até material em inglês eu consultei e nada.
Desde já obrigado pela força.