Home > Mobile >  How to get access to a binding to a WPF Binding on C# CodeBehind level
How to get access to a binding to a WPF Binding on C# CodeBehind level

Time:09-23

I am a VB.Net programmer and quite new to C#. I am at a point where im stuck. I want to make an app to create a quotation with Word. This Quotation should consist of two Word files. The Word files are Templates with Bookmarks, so writing to them should be no problem.

I want to have a WPF User Interface where the User can describe the Article and when clicking on a button the two Word files will be created.

I made the WPF User Interface and binded the Textboxes to a cl_Data.cs Class where are Properties such as : Description, FunctionName, etc.

My Problem: How can i access the Data from the User Interface from my Code Behinde to shift it to the Word files?

The Code: WPF: How i Bind it on .xaml level

    <Window.Resources>
        <!-- Binding the Data Class-->
        <local:Cl_Data x:Key="Data" 
                       Dealer="Test"
                       Costumer="Tester"
                       Machine="M***s"
                       PRJ="123456"
                       DeliveryTime="6"
                       Description="Managing different chucks, Saving position data of the linear sensor for chuck clamp unclamp position"
                       Operation="The operator can select a chuck form the chuck management and save the clamp and unclamp position and reuse this position for next time"
                       FunctionName="GeneratorAPP"
                       Requirements="API-Kit"
                       />
    </Window.Resources>

How i call it on .xaml level (same document) -> This works

    <Border BorderBrush="#FFB0F0FF" BorderThickness="1" Height="26">
                                            <TextBox x:Name="Tb_Dealer"  
                                            TextWrapping="Wrap" Text="{Binding Dealer,    UpdateSourceTrigger=PropertyChanged}" Width="auto" Foreground="#FFB0F0FF" BorderBrush="#00ABADB3" Background="Transparent" TextAlignment="Center" VerticalAlignment="Center" />
                                        </Border>

<Border BorderBrush="#FFB0F0FF" BorderThickness="1" Height="26">
                                            <TextBox x:Name="Tb_Dealer"                             TextWrapping="Wrap" Text="{Binding Dealer, UpdateSourceTrigger=PropertyChanged}" Width="auto" Foreground="#FFB0F0FF" BorderBrush="#00ABADB3" Background="Transparent" TextAlignment="Center" VerticalAlignment="Center" />
                                        </Border>

So my class cl_Data.cs looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;

namespace QuotationApp.Classes
{
    internal class Cl_Data : INotifyPropertyChanged
    {
        #region Descriptions
        private string _Dealer   ;

        public string Dealer
        {
            get { return _Dealer; }
            set 
            { _Dealer = value;
                OnPropertyChanged("Dealer");
            }
        }

        private string _Costumer;

        public string Costumer
        {
            get { return _Costumer; }
            set
            {
                _Costumer = value;
                OnPropertyChanged("Costumer");
            }
        }


        private string _Machine;

        public string Machine
        {
            get { return _Machine; }
            set 
            { 
                _Machine = value;
                OnPropertyChanged("Machine");
            }
        }

    
        

        private string _PRJ;

        public string PRJ
        {
            get { return _PRJ; }
            set { _PRJ = value; 
            OnPropertyChanged(PRJ);
            }
        }

        private string _DeliveryTime;

        public string DeliveryTime
        {
            get { return _DeliveryTime; }
            set { 
                _DeliveryTime = value;
                OnPropertyChanged("DeliveryTime");
            }
        }

        private string _Operation;
                
        public string Operation
        {
            get { return _Operation; }
            set { 
                _Operation = value;
                OnPropertyChanged("Operation");
            }
        }

        private string _Description;

        public string Description
        {
            get { return _Description; }
            set {
                _Description = value;
                OnPropertyChanged("Description");
            }
        }

        private string _FunctionName;

        public string FunctionName
        {
            get { return _FunctionName; }
            set { 
                _FunctionName = value;
                OnPropertyChanged("FunctionName");
            }
        }

        private string _Requirements;

        public string Requirements
        {
            get { return _Requirements; }
            set { 
                _Requirements = value;
                OnPropertyChanged("Requirements");
            }
        }
        #endregion

        #region Costs

        private double _HardwareCost;

        public double HardwareCost
        {
            get { return _HardwareCost; }
            set { 
                _HardwareCost = value;
                _CostTotal = CalcTotal();
                OnPropertyChanged("HardwareCost");
            }
        }

        private double _PersonalCost;

        public double PersonalCost
        {
            get { return _PersonalCost; }
            set { 
                _PersonalCost = value;
                _CostTotal = CalcTotal();
                OnPropertyChanged("PersonalCost");

            }
        }

        private double _TravelCost;

        public double TravelCost
        {
            get { return _TravelCost; }
            set { 
                _TravelCost = value;
                _CostTotal = CalcTotal();
                OnPropertyChanged("TravelCost");
            }
        }

        private double _CostTotal;

        public double CostTotal
        {
            get { return _CostTotal; }
            set { 
                _CostTotal = value;
                OnPropertyChanged("CostTotal");
            }
        }

       public double CalcTotal()
        {
            double total = 0;
            try
            {
              total = TravelCost   HardwareCost   PersonalCost;
                
               
                                
            }
            catch (Exception e)
            {

               MessageBox.Show("Error getting the total Value: "   e.Message);  
            }


            return total;
        }



        #endregion










        #region PropertyChangedEvents
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

So now i want to access these Data for example the Description (Data.Description) to process it to a word Bookmark. But how can i Access this Data on WPF level from CodeBehind?

Please be easy with me, i know this question is wierd but i googled 2 days now an i am starting to get frustrated. If this question is answered somewhere else, i would love to have the link to the answer.

Thanks in advance

CodePudding user response:

I made the most simple example as far as it came to my mind.
If you don't understand, ask questions about it.
I will try to answer.

using System;

namespace Core2022.Lexxy_B
{
    public class PersonDto
    {
        public int Id { get; }
        public string Name { get; }
        public int Age { get; }

        public PersonDto(int id, string name, int age)
        {
            if (Id < 0)
                throw new ArgumentOutOfRangeException(nameof(id));
            Id = id;
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentNullException(nameof(name));
            Name = name;
            if (age < 0)
                throw new ArgumentOutOfRangeException(nameof(age));
            Age = age;
        }
        public PersonDto(string name, int age)
            : this(0, name, age)
        {
            Id = -1;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

namespace Core2022.Lexxy_B
{
    public class PeopleModel
    {
        private readonly List<PersonDto> people = new List<PersonDto>()
        {
            new PersonDto(5, "Thomas", 25),
            new PersonDto(1, "Harry", 40),
        };
        public IReadOnlyList<PersonDto> GetPeople() => Array.AsReadOnly(people.ToArray());

        public void AddPerson(PersonDto person)
        {
            int id = people.LastOrDefault()?.Id ?? 0;
            do
            {
                id  ;
            } while (people.Any(p => p.Id == id));

            person = new PersonDto(id, person.Name, person.Age);
            people.Add(person);
            AddedPerson?.Invoke(this, person);
        }

        public event EventHandler<PersonDto>? AddedPerson;
    }
}
namespace Core2022.Lexxy_B
{
    public class PersonVM
    {
        public string? Name { get; set; }
        public int Age { get; set; }
    }
}
using Simplified;
using System.Collections.ObjectModel;

namespace Core2022.Lexxy_B
{

    public class PeopleViewModel : ViewModelBase
    {
        private readonly PeopleModel model = new PeopleModel();
        private string _mode = "view";

        public ObservableCollection<PersonDto> People { get; } = new ObservableCollection<PersonDto>();
        public string ViewMode { get => _mode; private set => Set(ref _mode, value); }
        public PeopleViewModel()
        {
            foreach (var person in model.GetPeople())
            {
                People.Add(person);
            }

            model.AddedPerson  = OnAddedPerson;
        }

        private void OnAddedPerson(object? sender, PersonDto newPerson)
        {
            People.Add(newPerson);
        }

        public RelayCommand AddPersonCommand => GetCommand<PersonVM>(AddPersonExecute, AddPersonCanExecute);
        private void AddPersonExecute(PersonVM person)
        {
            model.AddPerson(new PersonDto(person.Name ?? string.Empty, person.Age));
            ViewMode = "view";
        }

        private bool AddPersonCanExecute(PersonVM person)
        {
            return !string.IsNullOrWhiteSpace(person.Name) && person.Age >= 0;
        }
        public RelayCommand ExitAddingPersonCommand => GetCommand(() => ViewMode = "view");
        public RelayCommand BeginAddingPersonCommand => GetCommand(() => ViewMode = "add");
    }
}
<Window x:Class="Core2022.Lexxy_B.PeopleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Core2022.Lexxy_B"
        mc:Ignorable="d"
        Title="PeopleWindow" Height="450" Width="800"
        DataContext="{DynamicResource vm}">
    <Window.Resources>
        <local:PeopleViewModel x:Key="vm"/>
    </Window.Resources>
    <UniformGrid Columns="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ListBox x:Name="lBox" ItemsSource="{Binding People}" DisplayMemberPath="Name"/>
            <Button Grid.Row="1" Content="Go to Add Person" Padding="15 5" Margin="5"
                    Command="{Binding BeginAddingPersonCommand}">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ViewMode}" Value="add">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>
            <ContentControl x:Name="cp">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Style.Resources>
                            <DataTemplate x:Key="view.Template">
                                <local:PersonDetailsUC/>
                            </DataTemplate>
                            <DataTemplate x:Key="add.Template">
                                <local:AddPersonUC/>
                            </DataTemplate>
                        </Style.Resources>
                        <Setter Property="Content" Value="{Binding}"/>
                        <Setter Property="ContentTemplate" Value="{StaticResource add.Template}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ViewMode}" Value="view">
                                <Setter Property="Content" Value="{Binding SelectedItem, ElementName=lBox}"/>
                                <Setter Property="ContentTemplate" Value="{StaticResource view.Template}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
    </UniformGrid>
</Window>
<UserControl x:Class="Core2022.Lexxy_B.PersonDetailsUC"
             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:local="clr-namespace:Core2022.Lexxy_B"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             d:DataContext="{d:DesignInstance Type=local:PersonDto}">
    <UniformGrid Columns="1">
        <TextBlock Text="{Binding Id, StringFormat={}Id: {0}, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Text="{Binding Name, StringFormat={}Name: {0}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Text="{Binding Age, StringFormat={}Age: {0}, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </UniformGrid>
</UserControl>
<UserControl x:Class="Core2022.Lexxy_B.AddPersonUC"
             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:local="clr-namespace:Core2022.Lexxy_B"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <d:UserControl.DataContext>
        <local:PeopleViewModel/>
    </d:UserControl.DataContext>
    <UserControl.Resources>
        <local:PersonVM x:Key="person"/>
    </UserControl.Resources>
    <UniformGrid Columns="2">
        <TextBlock Text="Name" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBox Text="{Binding Name, Source={StaticResource person}}" VerticalAlignment="Center" Margin="10"/>
        <TextBlock Text="Age" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBox Text="{Binding Age, Source={StaticResource person}}" VerticalAlignment="Center" Margin="10"/>
        <Button Content="Add" Padding="15 5"  VerticalAlignment="Center" HorizontalAlignment="Center"
                Command="{Binding AddPersonCommand}"
                CommandParameter="{Binding Mode=OneWay, Source={StaticResource person}}"/>
        <Button Content="Exit" Padding="15 5"  VerticalAlignment="Center" HorizontalAlignment="Center"
                Command="{Binding ExitAddingPersonCommand}"/>
    </UniformGrid>
</UserControl>

Addition due to the Repository.

change Line 33 of Word.cs as i was unable to assign a relative path to open to word template

An example of the implementation of obtaining the full name of files by their path given by a relatively executable assembly.

  1. The file itself in the Project resources must have the properties "Content" - "Copy ...". I have a Russified Studio, so the screenshot is in Russian.
    The "bin" folder must not be included in the Project, otherwise, all its contents will also be included in the Assembly.
    enter image description here
    And here is the code for converting a relative path to an absolute one:
        public static readonly string ApplicationFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        public const string TestRelativeNameFile = @"Resources\Word\Test.docx";
        public static readonly string TestFullNameFile = Path.Combine(ApplicationFolder, TestRelativeNameFile);
        public static void GenerateLocalSolution()
        {
            try
            {
                WordApp = new Microsoft.Office.Interop.Word.Application();
                TestApp = WordApp.Documents.Open(TestFullNameFile);
            }

i bind the DataContext in MainWindow.cs (CodeBehind) to a Class and create a specific Instance of the Object which i access from every other class

This implementation doesn't play well with WPF and OOP. For example, in Designer mode (when you edit XAML in Studio), you have an empty value in the DataContext. And because of this, you can't use the Binding Builder in development.

If you have an instance of the Cl_Data class specific to each MainWindow instance, then it should be initialized in the Window's XAML:

    <Window.Resources>
        <classes:Cl_Data
            xmlns:classes="clr-namespace:QuotationApp.Classes"
            x:Key="data"/>
    </Window.Resources>

If the Cl_Data instance is the only one in the application sense and the entire session exists, then you can create it in the App resources:

    <Application.Resources>
        <classes:Cl_Data
            xmlns:classes="clr-namespace:QuotationApp.Classes"
            x:Key="data"/>
    </Application.Resources>

The window gets it in the DataContext in XAML too.
If its value is needed in the Code Behind, then it must be retrieved either by key or from the DataContext.

<Window x:Class="QuoteApp_EldHasp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ------------------
        Title="MainWindow" Height="900" Width="1800"
        DataContext="{DynamicResource data}">
        // Creating such a public property is a bad idea. But for now, I'm not getting involved.
        public /* static */ Cl_Data Data { get; } //= new Cl_Data();

        public MainWindow()
        {
            InitializeComponent();

            Data = (Cl_Data)DataContext;
            // Or
            Data = (Cl_Data)FindResource("data");
        }

Next note.
In the Word.GenerateLocalSolution() method, the line "descRange.Text = MainWindow.Data.Dealer" is a VERY STRONG OOP violation. The Word class contains logic for working with data (Domain Logic) - this is its "single-responsibility". And it should not work with UI elements. And "MainWindow.Data" is the View property! There are several options for correct implementation. One of them is to get the desired value in the parameter and let the one who called this method decide where to get it from.

        public static void GenerateLocalSolution(string text)
        {
            // Some Code
            descRange.Text = text; // MainWindow.Data.Dealer;
        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            var data = (Cl_Data)DataContext;
            Word.GenerateLocalSolution(data.Dealer);
        }

You should also replace clickers with commands. This will significantly improve the architecture of the application and make the code easier.

  • Related