Created
April 10, 2017 10:56
-
-
Save Coding-Enthusiast/89aaf9c20b11db18be2690eb05c35eb9 to your computer and use it in GitHub Desktop.
Testing IValueConverter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="TestIconvert.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:vm="clr-namespace:TestIconvert" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<vm:MainWindowViewModel/> | |
</Window.DataContext> | |
<Window.Resources> | |
<vm:CustomConverter x:Key="myConverter"/> | |
</Window.Resources> | |
<Grid> | |
<DataGrid ItemsSource="{Binding ModelAList}" AutoGenerateColumns="False"> | |
<DataGrid.Columns> | |
<DataGridTextColumn Header="ID" Binding="{Binding ID, Mode=TwoWay}"/> | |
<DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay}"/> | |
<DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=TwoWay}"/> | |
<DataGridTextColumn Header="ModelBID" Binding="{Binding ModelBID, Mode=TwoWay}"/> | |
<DataGridTextColumn Header="ModelB" Binding="{Binding ModelBID, Converter={StaticResource myConverter}}"/> | |
</DataGrid.Columns> | |
</DataGrid> | |
</Grid> | |
</Window> | |
/************************************************************************/ | |
using System; | |
using System.Collections.Generic; | |
using System.Windows.Data; | |
namespace TestIconvert | |
{ | |
class CustomConverter : IValueConverter | |
{ | |
public Dictionary<long, ModelB> ModelBDict | |
{ | |
get | |
{ | |
// Load from database or whatever here: | |
Dictionary<long, ModelB> result = new Dictionary<long, ModelB>(); | |
result.Add(1200, new ModelB() { Description = "modelB desc", ID = 50, Name = "MB name" }); | |
return result; | |
} | |
} | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
long i = (long)value; | |
try | |
{ | |
ModelB mb = ModelBDict[i]; | |
return mb.Name; | |
} | |
catch (Exception) | |
{ | |
return "Not Found in database!"; | |
} | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} | |
/************************************************************************/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Collections.ObjectModel; | |
namespace TestIconvert | |
{ | |
public class MainWindowViewModel : InpcBase // InpcBase is the implementation of INotifyPropertyChanged | |
{ | |
public MainWindowViewModel() | |
{ | |
ModelA m = new ModelA() { Description = "desc", ID = 100, ModelBID = 1200, Name = "MA Name" }; | |
ModelAList = new ObservableCollection<ModelA>(); | |
ModelAList.Add(m); | |
ModelA m2 = new ModelA() { Description = "desc2", ID = 200, ModelBID = 1500, Name = "MA2 Name" }; | |
ModelAList.Add(m2); | |
} | |
public ObservableCollection<ModelA> ModelAList { get; set; } | |
} | |
} | |
/************************************************************************/ | |
namespace TestIconvert | |
{ | |
public class ModelA : InpcBase | |
{ | |
public long ID { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
public long ModelBID { get; set; } | |
} | |
} | |
/************************************************************************/ | |
namespace TestIconvert | |
{ | |
public class ModelB | |
{ | |
public long ID { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment