简体   繁体   English

如何访问与 C# 代码隐藏级别上的 WPF 绑定的绑定

[英]How to get access to a binding to a WPF Binding on C# CodeBehind level

I am a VB.Net programmer and quite new to C#.我是一名 VB.Net 程序员,对 C# 很陌生。 I am at a point where im stuck.我正处于我陷入困境的地步。 I want to make an app to create a quotation with Word.我想制作一个应用程序来使用 Word 创建报价单。 This Quotation should consist of two Word files.此报价单应包含两个 Word 文件。 The Word files are Templates with Bookmarks, so writing to them should be no problem. Word 文件是带有书签的模板,因此写入它们应该没有问题。

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.我想要一个 WPF 用户界面,用户可以在其中描述文章,单击按钮时将创建两个 Word 文件。

I made the WPF User Interface and binded the Textboxes to a cl_Data.cs Class where are Properties such as: Description, FunctionName, etc.我制作了 WPF 用户界面并将文本框绑定到 cl_Data.cs Class 属性,例如:描述、功能名称等。

My Problem: How can i access the Data from the User Interface from my Code Behinde to shift it to the Word files?我的问题:我如何从我的 Code Behinde 从用户界面访问数据以将其转换为 Word 文件?

The Code: WPF: How i Bind it on.xaml level代码:WPF:我如何绑定它。xaml 级别

    <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我如何称呼它。xaml 级别(同一文档)-> 这有效

    <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:所以我的 class cl_Data.cs 看起来像:

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.所以现在我想访问这些数据,例如描述(Data.Description)以将其处理为单词书签。 But how can i Access this Data on WPF level from CodeBehind?但是我如何从 CodeBehind 访问 WPF 级别的这些数据?

Please be easy with me, i know this question is wierd but i googled 2 days now an i am starting to get frustrated.请对我放轻松,我知道这个问题很奇怪,但我现在用谷歌搜索了 2 天,我开始感到沮丧。 If this question is answered somewhere else, i would love to have the link to the answer.如果这个问题在其他地方得到了回答,我很想有答案的链接。

Thanks in advance提前致谢

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM