The binding also has another mechanism called data conversion, which can be data Converter when the data associated with the source side path is inconsistent with the target attribute data type. When WPF cannot automatically convert data types, it can only write converter by itself, by creating a class and having the class implement the IValueConverter interface. The Convert method is called when the data flows from the source of the binding to target, whereas the Convertback method is called.
The Mode property of the binding object affects the invocation of both methods. If mode is TwoWay or the default behavior is consistent with TwoWay, two methods can be called, and if mode is OneWay or the default behavior is consistent with OneWay, only the Convert method is called. Examples are as follows:
Xaml:
<Window.Resources>
<local:categorytosourceconverter x:key= "CTS"/>
<local:statetonullablboolconverter x:key= "STNB"/>
</Window.Resources>
<stackpanel background= "LightBlue" >
<listbox x:name= "Listboxplane" height= "margin=" 5 ">
<ListBox.ItemTemplate>
<DataTemplate>
<stackpanel orientation= "Horizontal" >
<image width= "height=" "source=" {Binding path=category, Converter={staticresource resourcekey=cts}} "/>
<textblock text= "{Binding path=name}" width= "margin=", 0 "/>
<checkbox isthreestate= "True" ischecked= "{Binding path=state, Converter={staticresource RESOURCEKEY=STNB}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<button x:name= "Buttonload" content= "Load" height= "margin=" 5, 0 "click=" Buttonload_click "/>
<button x:name= "Buttonsave" content= "Save" height= "margin=" 5, 5 "click=" Buttonsave_click "/>
</StackPanel>
C # code:
public enum Category
{
Bomber,
Fighter
}
public enum State
{
Available,
Locked,
Unknown
}
public class Plane
{
Public category category {get; set;}
public string Name {get; set;}
Public state State {get; set;}
}
public class Categorytosourceconverter:ivalueconverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Category C = (category) value;
Switch (c)
{
Case Category.bomber:
return @ "\icons\bomber.png";
Case Category.fighter:
return @ "\icons\fighter.png";
Default
return null;
}
}
public object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
}
public class Statetonullablboolconverter:ivalueconverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
state S = (state) value;
Switch (s)
{
Case State.available:
return true;
Case state.locked:
return false;
Case State.unknown:
Default
return null;
}
}
public object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? NB = (bool?) Value
Switch (NB)
{
Case true:
return state.available;
Case false:
return state.locked;
Case NULL:
Default
return state.unknown;
}
}
}
<summary>
The interactive logic of MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();
}
private void Buttonload_click (object sender, RoutedEventArgs e)
{
list<plane> planelist = new list<plane>
{
New plane{category = category.bomber, Name = "B--1", state = State.unknown},
New plane{category = category.bomber, Name = "B--2", state = State.unknown},
New plane{category = category.fighter, Name = "F--22", state = State.unknown},
New plane{category = category.fighter, Name = "su--47", state = State.unknown},
New plane{category = category.bomber, Name = "b--52", state = State.unknown},
New plane{category = category.fighter, Name = "j--10", state = State.unknown}
};
This.listBoxPlane.ItemsSource = planelist;
}
private void Buttonsave_click (object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder ();
foreach (Plane p in Listboxplane.items)
{
Sb. AppendFormat (String. Format ("Category = {0}, Name = {1}, state = {2}", P.category, P.name, p.state));
}
File.writealltext (@ "D:\PlaneList.txt", sb.) ToString ());
}
}
This article is from the "Ink Pool small" blog, please be sure to keep this source http://306702895.blog.51cto.com/8366753/1618825
The data converter--notes of WPF (2015.03.09)