Difference between revisions of "Databinding"

From truxwiki.com
Jump to navigation Jump to search
(Created page with "This page documents the Navigation/Routing of the application, or how each "page" is navigated. This document is intended to detail the initial resources that are used when th...")
 
(added d:DataContext and BindingProxy info)
Line 7: Line 7:
 
:# [[ Databinding ]]
 
:# [[ Databinding ]]
  
== Main Files ==
+
== Databinding ==
 +
 
 +
::There are multiple ways to bind data. I'll describe the most complex and most used ones below.
 +
 
 +
=== d:DataContext ===
 +
::Some XAMLs will have references using the <b>d:DataContext</b> tag. This is more of a developer's note. It does not actually bind anything. It's useful to let the developer know where to look. If a xaml doesn't have one, it's probably loaded in through BindingProxy. That is not always the case.
 +
<div style="margin-left:3rem;"> 
 +
  <Grid x:Class="TruxtonClient.Views.Dashboard"
 +
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 +
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 +
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 +
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 +
      xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
 +
      xmlns:controls="clr-namespace:TruxtonClient.Controls;assembly="
 +
      xmlns:uicore="clr-namespace:Truxton;assembly=Truxton.UI.Core"
 +
      xmlns:converters="clr-namespace:TruxtonClient.Converters;assembly="
 +
      xmlns:behaviors="clr-namespace:TruxtonClient.Behaviors;assembly="
 +
      xmlns:local="clr-namespace:TruxtonClient;assembly="
 +
      xmlns:models="clr-namespace:TruxtonClient.Models;assembly="
 +
      xmlns:MahAppsControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
 +
      xmlns:viewModels="clr-namespace:TruxtonClient.ViewModels;assembly="
 +
      xmlns:ig="http://schemas.infragistics.com/xaml"
 +
      xmlns:vm="clr-namespace:TruxtonClient.ViewModels"
 +
      <b>d:DataContext="{d:DesignInstance Type=vm:DashboardViewModel}"</b>
 +
      mc:Ignorable="d"
 +
      d:DesignHeight="600"
 +
      d:DesignWidth="800"
 +
      Background="White">
 +
</div>
 +
 
 +
=== BindingProxy ===
 +
::BindingProxy is a tool used to reference a specific Databinding source elsewhere in the code where it may not be readily available. For example, a template resource can be used in multiple Views. The BindingProxy can be used to pass a specific View's DataContext into the template instance.
  
 
::{| class="wikitable"
 
::{| class="wikitable"
! File
+
! Example
! Summary
 
! Path
 
 
|-
 
|-
| PageManager.cs
+
| Views/Home/Home.xaml
| This class manages the navigation tools such as : page loading, the bread crumbs, last page loaded,
+
  <Grid x:Class="TruxtonClient.Views.Home"
| \Truxton\Client\TruxtonClient\TruxtonClient\PresentationCore\PageManager.cs
+
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 +
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 +
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 +
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 +
      mc:Ignorable="d"
 +
      d:DesignHeight="300"
 +
      d:DesignWidth="600"
 +
      Background="White"
 +
      xmlns:vm="clr-namespace:TruxtonClient.ViewModels"
 +
      <b>d:DataContext="{d:DesignInstance Type=vm:HomeViewModel}"></b>
 +
    <DockPanel SnapsToDevicePixels="True">
 +
        <!-- left side nav -->
 +
        <b><ContentControl Content="{Binding Navigation}"
 +
                        ContentTemplate="{StaticResource LeftSideNav}" /></b>
 +
       
 +
        <!-- right side content-->
 +
        <Grid Name="ChildContainer" />
 +
    </DockPanel>
 +
  </Grid>
 
|-
 
|-
| HomeViewModel.cs
+
| TruxtonClient/Templates/LeftSideNav.xaml ~Line 453
| Adds the NavItems to the Main Menu
+
  <DataTemplate x:Key="LeftSideNav">
| \Truxton\Client\TruxtonClient\TruxtonClient\ViewModels\HomeViewModel.cs
+
  <Border Style="{StaticResource LeftSidePanel}">
 +
      <Border.Resources>
 +
          <b><uicore:BindingProxy x:Key="BindingProxy"
 +
                                  Data="{Binding}" /></b>
 +
          <DataTemplate x:Key="LeftSideNavListItem">
 +
              <Button Style="{StaticResource SideNavButton}"
 +
                      <b>Command="{Binding Data.Select,
 +
                          Source={StaticResource BindingProxy}}"</b>
 +
                      CommandParameter="{Binding}"
 +
                      Tag="{Binding Name}"
 +
                      ToolTip="{Binding ToolTip}">
 
|-
 
|-
 
|}
 
|}
 +
 +
In the example above, the Home View loads the LeftSideNav template and passes its Navigation DataContext to the LeftSideNav template.

Revision as of 11:09, 2 November 2021

This page documents the Navigation/Routing of the application, or how each "page" is navigated. This document is intended to detail the initial resources that are used when the application loads each page or view.

UI Guide Menu

  1. Entry Point
  2. Navigation and Routing
  3. Databinding

Databinding

There are multiple ways to bind data. I'll describe the most complex and most used ones below.

d:DataContext

Some XAMLs will have references using the d:DataContext tag. This is more of a developer's note. It does not actually bind anything. It's useful to let the developer know where to look. If a xaml doesn't have one, it's probably loaded in through BindingProxy. That is not always the case.
  <Grid x:Class="TruxtonClient.Views.Dashboard"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
     xmlns:controls="clr-namespace:TruxtonClient.Controls;assembly="
     xmlns:uicore="clr-namespace:Truxton;assembly=Truxton.UI.Core"
     xmlns:converters="clr-namespace:TruxtonClient.Converters;assembly="
     xmlns:behaviors="clr-namespace:TruxtonClient.Behaviors;assembly="
     xmlns:local="clr-namespace:TruxtonClient;assembly="
     xmlns:models="clr-namespace:TruxtonClient.Models;assembly="
     xmlns:MahAppsControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
     xmlns:viewModels="clr-namespace:TruxtonClient.ViewModels;assembly="
     xmlns:ig="http://schemas.infragistics.com/xaml"
     xmlns:vm="clr-namespace:TruxtonClient.ViewModels"
     d:DataContext="{d:DesignInstance Type=vm:DashboardViewModel}"
     mc:Ignorable="d"
     d:DesignHeight="600"
     d:DesignWidth="800"
     Background="White">

BindingProxy

BindingProxy is a tool used to reference a specific Databinding source elsewhere in the code where it may not be readily available. For example, a template resource can be used in multiple Views. The BindingProxy can be used to pass a specific View's DataContext into the template instance.
Example
Views/Home/Home.xaml
  <Grid x:Class="TruxtonClient.Views.Home"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     mc:Ignorable="d"
     d:DesignHeight="300"
     d:DesignWidth="600"
     Background="White"
     xmlns:vm="clr-namespace:TruxtonClient.ViewModels"
     d:DataContext="{d:DesignInstance Type=vm:HomeViewModel}">
   <DockPanel SnapsToDevicePixels="True">
       <ContentControl Content="{Binding Navigation}"
                       ContentTemplate="{StaticResource LeftSideNav}" />
       
       <Grid Name="ChildContainer" />
   </DockPanel>
  </Grid>
TruxtonClient/Templates/LeftSideNav.xaml ~Line 453
  <DataTemplate x:Key="LeftSideNav">
  <Border Style="{StaticResource LeftSidePanel}">
      <Border.Resources>
          <uicore:BindingProxy x:Key="BindingProxy"
                                 Data="{Binding}" />
          <DataTemplate x:Key="LeftSideNavListItem">
              <Button Style="{StaticResource SideNavButton}"
                      Command="{Binding Data.Select, 
                          Source={StaticResource BindingProxy}}"
                      CommandParameter="{Binding}"
                      Tag="{Binding Name}"
                      ToolTip="{Binding ToolTip}">

In the example above, the Home View loads the LeftSideNav template and passes its Navigation DataContext to the LeftSideNav template.