Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:wpf:data_binding [2011/05/27 13:00] smayr [DataContext] |
swdev:dotnet:wpf:data_binding [2017/06/27 17:22] (current) smayr [Add Tracing to Output Window] |
||
---|---|---|---|
Line 17: | Line 17: | ||
To specify additional parameters to the converter. | To specify additional parameters to the converter. | ||
+ | Example: | ||
+ | <code xml> | ||
+ | < | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | . . . | ||
+ | < | ||
+ | Visibility=" | ||
+ | Converter={StaticResource InstrumentSideDetectedToVisibilityConverter}, | ||
+ | ConverterParameter={x: | ||
+ | Content=" | ||
+ | . . . | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Custom type declared in Acme.App.Types.dll assembly: | ||
+ | <code csharp> | ||
+ | public enum TSide | ||
+ | { | ||
+ | Left | ||
+ | Right | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Custom ValueConverter declared in Acme.App.ViewModel.dll assembly (view model): | ||
+ | <code csharp> | ||
+ | /// | ||
+ | /// < | ||
+ | /// Class TInstrumentSideDetectedToVisibilityConverter | ||
+ | /// </ | ||
+ | /// | ||
+ | public class TInstrumentSideDetectedToVisibilityConverter : IValueConverter | ||
+ | { | ||
+ | public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | ||
+ | { | ||
+ | Visibility _visibility; | ||
+ | |||
+ | if ( | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Left) | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Both) | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.None) | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Right) && ((TSide)parameter == TSide.Right) || | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Both) | ||
+ | ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.None) | ||
+ | ) | ||
+ | { | ||
+ | _visibility = Visibility.Visible; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | _visibility = Visibility.Collapsed; | ||
+ | } | ||
+ | return _visibility; | ||
+ | } | ||
+ | |||
+ | public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | ||
+ | { | ||
+ | throw new NotImplementedException(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
==== UpdateSourceTrigger ==== | ==== UpdateSourceTrigger ==== | ||
To specify when the target property is updated. | To specify when the target property is updated. | ||
Line 130: | Line 196: | ||
If the path contains an index, then use: | If the path contains an index, then use: | ||
<code xml> | <code xml> | ||
- | < | + | < |
- | < | + | < |
</ | </ | ||
<code csharp> | <code csharp> | ||
- | ccDetails.SetBinding(ContentControl.ContentProperty, | + | ccDetails.SetBinding(ContentControl.ContentProperty, |
- | ccDetails.SetBinding(ContentControl.ContentProperty, | + | ccDetails.SetBinding(ContentControl.ContentProperty, |
</ | </ | ||
== Data Providers == | == Data Providers == | ||
Line 209: | Line 275: | ||
<code xml> | <code xml> | ||
< | < | ||
- | < | + | < |
</ | </ | ||
</ | </ | ||
+ | |||
+ | Declaring a data source with parameters vs no parameters: | ||
+ | <code xml> | ||
+ | <!--Data Source--> | ||
+ | < | ||
+ | <!--This Method creates the object, then later it assigns the field (ie. field is not assign during construction)--> | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
Define the data type in library. | Define the data type in library. | ||
Line 224: | Line 305: | ||
public class TProductSpecificationList : List< | public class TProductSpecificationList : List< | ||
{ | { | ||
- | public TProductSpecificationList() | + | |
+ | |||
+ | // Parameterless Constructor | ||
+ | | ||
{ | { | ||
- | this.Add(" | + | |
- | this.Add(" | + | } |
+ | // Constructor with Parameters | ||
+ | public TProductSpecificationList(TCustomCompany aCustomCompany) | ||
+ | { | ||
+ | if (CustomCompany == TCustomCompany.Acme) | ||
+ | { | ||
+ | this.Add(" | ||
+ | | ||
+ | } | ||
} | } | ||
} | } | ||
Line 299: | Line 391: | ||
</ | </ | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | == Clearing Binding == | ||
+ | |||
+ | To clear data binding programmatically (for a '' | ||
+ | <code csharp> | ||
+ | BindingOperations.ClearBinding(txtName, | ||
</ | </ | ||
Line 1061: | Line 1160: | ||
Data binding to properties that do not match the type require a value converter (using '' | Data binding to properties that do not match the type require a value converter (using '' | ||
<code csharp> | <code csharp> | ||
+ | // using System.Windows.Data; | ||
public class TBoolToVisibilityConverter: | public class TBoolToVisibilityConverter: | ||
{ | { | ||
Line 1105: | Line 1205: | ||
A value converter for an image path that needs the full path prefixed to it: | A value converter for an image path that needs the full path prefixed to it: | ||
<code csharp> | <code csharp> | ||
+ | // using System.Windows.Data; | ||
public class TImgPathConverter : IValueConverter | public class TImgPathConverter : IValueConverter | ||
{ | { | ||
Line 1136: | Line 1237: | ||
Microsoft' | Microsoft' | ||
<code csharp> | <code csharp> | ||
+ | // using System.Windows; | ||
+ | // using System.Windows.Data; | ||
[ValueConversion(typeof(DateTime), | [ValueConversion(typeof(DateTime), | ||
public class DateConverter : IValueConverter | public class DateConverter : IValueConverter | ||
Line 1317: | Line 1420: | ||
Source: [[http:// | Source: [[http:// | ||
+ | == Multi-Binding == | ||
+ | |||
+ | To display 2 different variables in a same field (eg: 3.14 = May 5, 2012): | ||
+ | <code xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <Binding Path=" | ||
+ | <Binding Path=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
= Debugging = | = Debugging = | ||
+ | |||
+ | == Add Tracing to Output Window == | ||
+ | |||
+ | 1. Add '' | ||
+ | <code xml> | ||
+ | <Window x: | ||
+ | xmlns=" | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | Title=" | ||
+ | < | ||
+ | < | ||
+ | diag: | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | 2. Add a value converter to the binding, to be able to put a break point in debugger. | ||
+ | |||
+ | See more: | ||
+ | * [[https:// | ||
+ | |||
+ | == Enable Debug Output == | ||
* Enable WPF debug output. In Visual Studio, Options > Debugging > Output Window > WPF Trace Settings > Data Binding > All. | * Enable WPF debug output. In Visual Studio, Options > Debugging > Output Window > WPF Trace Settings > Data Binding > All. | ||
* Add a high TraceLevel to your binding: <code csharp> | * Add a high TraceLevel to your binding: <code csharp> | ||
Line 1329: | Line 1469: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | |||
= See Also = | = See Also = |