Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:data_binding [2012/03/12 12:08]
smayr [Debugging]
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>
 +<UserControl...
 +   xmlns:viewmodel="clr-namespace:Acme.App.ViewModel;assembly=Acme.App.ViewModel"
 +   xmlns:local="clr-namespace:Acme.App.Types;assembly=Acme.App.Types"
 +   ...>
 +
 +<UserControl.Resources>
 +  <viewmodel:TInstrumentSideDetectedToVisibilityConverter x:Key="InstrumentSideDetectedToVisibilityConverter"/>
 +</UserControl.Resources>
 +. . .
 +<ToggleButton 
 +  Visibility="{Binding Path=Fitting.DetectedSide, 
 +    Converter={StaticResource InstrumentSideDetectedToVisibilityConverter}, 
 +    ConverterParameter={x:Static local:TSide.Right}}"
 +  Content="An Environment">
 +. . .
 +</UserControl>  
 +</code>                                  
 +
 +Custom type declared in Acme.App.Types.dll assembly:
 +<code csharp>
 +public enum TSide
 +{
 +  Left
 +  Right
 +}
 +</code>
 +
 +Custom ValueConverter declared in Acme.App.ViewModel.dll assembly (view model):
 +<code csharp>
 +///============================================================================================
 +/// <summary>
 +/// Class TInstrumentSideDetectedToVisibilityConverter
 +/// </summary>
 +///============================================================================================
 +public class TInstrumentSideDetectedToVisibilityConverter : IValueConverter
 +{
 +    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 +    {
 +        Visibility _visibility;
 +
 +        if (
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Left)  && ((TSide)parameter == TSide.Left)  ||
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Both)  && ((TSide)parameter == TSide.Left)  ||
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.None)  && ((TSide)parameter == TSide.Left)  ||
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Right) && ((TSide)parameter == TSide.Right) ||
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.Both)  && ((TSide)parameter == TSide.Right) ||
 +            ((TInstrumentDetectedResults)value == TInstrumentDetectedResults.None)  && ((TSide)parameter == TSide.Right)
 +           )
 +        {
 +            _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();
 +    }
 +}
 +</code>    
 ==== UpdateSourceTrigger ==== ==== UpdateSourceTrigger ====
 To specify when the target property is updated. To specify when the target property is updated.
Line 209: Line 275:
 <code xml> <code xml>
   <UserControl.Resources>   <UserControl.Resources>
-    <local:TProductSpecificationList x:Key="objDataProviderProductSpecs" />+    <local:TProductSpecificationList x:Key="objDataProviderProductSpecs" />  
   </UserControl.Resources>     </UserControl.Resources>  
 </code> </code>
 +
 +Declaring a data source with parameters vs no parameters:
 +<code xml>
 +<!--Data Source-->
 +<!--Creates a simple data source object (but requires parameterless constructor)-->
 +<!--This Method creates the object, then later it assigns the field (ie. field is not assign during construction)-->
 +<prodspecs:TProductSpecificationList x:Key="ProdListDataSource" CustomCompany="Acme" />
 +    
 +<!--Creates data source object, and allow passing data into a constructor -->
 +<ObjectDataProvider x:Key="ProdListDataSource" ObjectType="{x:Type prodspecs:TProductSpecificationList}">
 +    <ObjectDataProvider.ConstructorParameters>
 +        <custom:TCustomCompany>Acme</custom:TCustomCompany>
 +    </ObjectDataProvider.ConstructorParameters>
 +</ObjectDataProvider>
 +</code>        
  
 Define the data type in library.  Eg: Acme.Products.Specification.dll Define the data type in library.  Eg: Acme.Products.Specification.dll
Line 224: Line 305:
   public class TProductSpecificationList : List<string>   public class TProductSpecificationList : List<string>
   {   {
-    public TProductSpecificationList()+    public TCustomCompany CustomCompany = TCustomCompany.Acme; 
 +     
 +    // Parameterless Constructor 
 +    public TProductSpecificationList(): this(TCustomCompany.Acme)
     {     {
-        this.Add("Bicycle"); +         
-        this.Add("Tricyle");+    } 
 +    // Constructor with Parameters 
 +    public TProductSpecificationList(TCustomCompany aCustomCompany) 
 +    { 
 +        if (CustomCompany == TCustomCompany.Acme) 
 +        { 
 +           this.Add("Bicycle"); 
 +           this.Add("Tricyle"); 
 +        }
     }     }
   }   }
Line 299: Line 391:
    </ListBox>    </ListBox>
 </Grid> </Grid>
 +</code>
 +
 +== Clearing Binding ==
 +
 +To clear data binding programmatically (for a ''TextBox'' called ''txtName''):
 +<code csharp>
 +BindingOperations.ClearBinding(txtName, TextBox.TextProperty);
 </code> </code>
  
Line 1336: Line 1435:
  
 = Debugging = = Debugging =
 +
 +== Add Tracing to Output Window ==
 +
 +1. Add ''diag:PresentationTraceSources.TraceLevel=High'' to your binding.  For example:
 +<code xml>
 +<Window x:Class="WpfApplication1.MainWindow"
 +        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 +        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 +        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
 +        Title="MainWindow" Height="350" Width="525">
 +    <Grid>
 +        <TextBlock Text="{Binding ThereIsNoDataContext, 
 +            diag:PresentationTraceSources.TraceLevel=High}"/>
 +    </Grid>
 +</Window>
 +</code>
 +
 +2. Add a value converter to the binding, to be able to put a break point in debugger.
 +
 +See more:
 +  * [[https://spin.atomicobject.com/2013/12/11/wpf-data-binding-debug/|How To Debug Data Binding Issues in WPF]]
 +
 +== 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>PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);</code>   * Add a high TraceLevel to your binding: <code csharp>PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);</code>
Line 1347: Line 1469:
   * [[http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runtim|Tools for Debugging Data Binding at runtime]]   * [[http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runtim|Tools for Debugging Data Binding at runtime]]
   * [[http://bea.stollnitz.com/blog/?p=52|Bea Stollnitz: How can I debug WPF bindings?]]   * [[http://bea.stollnitz.com/blog/?p=52|Bea Stollnitz: How can I debug WPF bindings?]]
 +  * [[https://spin.atomicobject.com/2013/12/11/wpf-data-binding-debug/|How To Debug Data Binding Issues in WPF]]
 +
 +
 = See Also = = See Also =
   * [[http://msdn2.microsoft.com/en-us/library/ms752347(VS.85).aspx|MSDN Data Binding Overview]]   * [[http://msdn2.microsoft.com/en-us/library/ms752347(VS.85).aspx|MSDN Data Binding Overview]]