【发布时间】:2022-09-23 16:55:22
【问题描述】:
我是一名 VB.Net 程序员,对 C# 很陌生。我正处于我陷入困境的地步。 我想制作一个应用程序来使用 Word 创建报价单。此报价单应包含两个 Word 文件。 Word 文件是带有书签的模板,因此写入它们应该没有问题。
我想要一个 WPF 用户界面,用户可以在其中描述文章,当单击一个按钮时,将创建两个 Word 文件。
我制作了 WPF 用户界面并将文本框绑定到 cl_Data.cs 类,其中的属性包括:描述、函数名等。
我的问题: 如何从我的 Code Behinde 的用户界面访问数据以将其转换为 Word 文件?
编码: 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>
我如何在 .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>
所以我的类 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
}
}
所以现在我想访问这些数据,例如描述(Data.Description)以将其处理为单词书签。但是我如何从 CodeBehind 访问 WPF 级别的这些数据?
请对我放轻松,我知道这个问题很奇怪,但我现在用谷歌搜索了 2 天,我开始感到沮丧。如果这个问题在其他地方得到了回答,我很想有答案的链接。
提前致谢
-
正确实施后,您不应使用 Code Behind 中的数据——这违反了 OOP 和 SOLID 的原则。处理数据的所有逻辑都应该在模型中。实际上,ViewModel 是在其属性中反映模型的代理。对于简单的任务,可以创建组合的 Model + ViewModel 类。
-
好吧,假设我会为此做一个模型。如何从 UI 获取数据?那是我的问题。
-
绑定。您已在代码中设置它们。我假设一旦你使用它们,你就会知道它们是如何工作的。当更改 TextBox 的值时,此更改将自动传递到 Dealer 属性。 GUI 中的用户操作主要通过命令传递给 ViewModel(即您的 Cl_Data 类)。并且 Execute 方法中的命令可以获取其参数和 ViewModel 属性/字段。
-
嘿@EldHasp,是的。问题是我不知道如何访问在 cl_data.cs 的 MainWindow.xaml 中创建的对象 \"Data\"。我想要类似的东西。书签 1 = 数据。描述。
-
所以在我想使用 Data 属性的模型中。如何从我创建的对象中获取数据?
标签: c# wpf data-binding