我對 C# 很陌生,所以我一直在努力解決這個問題,我不知道我應該做什么......
我想將我的類 ( sum interestRate_1) 中的 2 個(或更多)值與其他計算相加,并在ListView我創建到我的ObservableCollections-list 的每個類實體的各自列中顯示結果。它ListView本身受ItemSource所有創建的物件約束并包含所有Cust試圖對其執行計算的物件。
我對 XAML 完全陌生,所以我不知道它是如何作業的。人們告訴我使用轉換器,但我不確定它意味著什么......
這是我嘗試過的:
XAML 宣告:
<Window x:Class="MyProject.MainWindow"
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:MyProject"
mc:Ignorable="d"
Title="MyProject (beta)" Height="450" Width="1000" MinWidth="1000" MinHeight="450" ResizeMode="CanResize">
XAML ListView:
<ListView x:Name="ListTest" Margin="10,150,10,66" Background="#FF0077B6" Foreground="White" MouseDoubleClick="ListTest_MouseDoubleClick" SelectionChanged="ListTest_SelectionChanged" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding CustList}" Grid.RowSpan="2">
<ListView.Effect>
<DropShadowEffect/>
</ListView.Effect>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="136" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Amount (€)" Width="80" DisplayMemberBinding="{Binding Sum}"/>
<GridViewColumn Header="Amount With Interest (€)" Width="80">
<MultiBinding Converter="{StaticResource AddConverter}">
<Binding Path="Sum"/>
<Binding Path="interestRate_1"/>
</MultiBinding>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
C# 轉換器 物件串列Cust:
namespace MyProject
{
public class AddConverter : IMultiValueConverter
{
public object Convert(object[] values, Type tagetType, object parameter, CultureInfo culture)
{
var decimalValues = values.Cast<decimal>().ToArray();
var resultSum = decimalValues.Sum().ToString();
return resultSum;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window
{
public ObservableCollection<Cust> CustList = new ObservableCollection<Cust>();
public MainWindow()
{
InitializeComponent();
ListTest.ItemsSource = CustList;
}
//...
相關類屬性/屬性:
public class Cust
{
public Name {get; set;}
private decimal sum;
public decimal Sum
{
get { return sum; }
set { sum = value; }
}
public decimal interestRate_1 { get; set; } = 3.0M;
public decimal interestRate_2 { get; set; } = 5.0M;
// ...
我所知道的是 XAML 無法從 .cs 檔案中找到轉換器,并且Header我在嘗試轉換的列中使用了兩次(我不明白)。我完全迷失在這里了嗎?
uj5u.com熱心網友回復:
歡迎來到 xaml 和 c# 的土地:)
WPF 或 XAML 和 C# 的關鍵概念是 MVVM,它代表模型視圖 ViewModel 在您的示例中,Cust 類將是模型,后面的代碼的 XAML 將是視圖。缺少的是 ViewModel。這是您的代碼和您的視圖之間的連接。
在這里查看對我來說很重要的事情
- 您應該查找 MVVM 和系結的概念:
- cust 將是模型
- 轉換器通常用于系結值 eG
- if(value == null) return Visibility.Collapsed 所以我不認為它是適合這里作業的工具。
- 您可以考慮在 listView 中使用 DataGrid 控制元件而不是 GridView
- 如果你想使用 ListView 你必須搜索模板(特別是 ItemTemplates)
- 如果您想更改您的值并更新您的視圖,您必須查找 INotifyPropertyChanged 或找到一個基本實作來設定您的屬性。
但無論如何,這是解決您問題的方法:
<Window x:Class="WpfApp1.MainWindow"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<DataGrid ItemsSource="{Binding Custs}"/>
<Label Content="Result"/>
<Label Content="{Binding ResultSum}"/>
</StackPanel>
</Window>
所以這是一個視圖模型的基本示例
public class MainWindowViewModel
{
public ObservableCollection<Cust> Custs { get; set; }
public decimal ResultSum { get; set; }
public MainWindowViewModel()
{
Custs = new ObservableCollection<Cust>
{
new Cust
{
Name = "Joe",
Sum = 1.2M,
},
new Cust
{
Name = "Jane",
Sum = 3.2M,
},
};
ResultSum = Custs.Sum(cust => cust.Sum);
}
}
以及如何使用它
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
還有你的 Cust 類
public class Cust
{
public string Name
{
get;
set;
}
private decimal sum;
public decimal Sum
{
get { return sum; }
set { sum = value; }
}
public decimal interestRate_1 { get; set; } = 3.0M;
public decimal interestRate_2 { get; set; } = 5.0M;
}
結果看起來像這樣
uj5u.com熱心網友回復:
找不到轉換器,因為沒有它的實體。在 XAML 中的任何資源字典中創建一個實體,例如應用程式資源或在ListView.Resources.
<ListView.Resources>
<local:AddConverter x:Key="AddConverter"/>
</ListView.Resources>
的系結ItemsSource是多余的,因為您已經在代碼、使用或其他中分配了它。
ItemsSource="{Binding CustList}"
ListTest.ItemsSource = CustList;
但是,ItemSource只有在正確設定資料背景關系時,系結才會起作用,例如:
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
或者在這樣的 XAML 標記中。
<Window x:Class="MyProject.MainWindow"
...
DataContext="{Binding RelativeSource={RelativeSource Self}}">
最后,它說您分配了兩次標題,因為分配的直接內容也GridViewColumn分配了Header屬性。改為顯式使用該DisplayMemberBinding屬性。
<GridViewColumn Header="Amount With Interest (€)" Width="80">
<GridViewColumn.DisplayMemberBinding>
<MultiBinding Converter="{StaticResource AddConverter}">
<Binding Path="Sum"/>
<Binding Path="interestRate_1"/>
</MultiBinding>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
這是您的完整代碼ListView。
<ListView x:Name="ListTest" Margin="10,150,10,66" Background="#FF0077B6" Foreground="White" MouseDoubleClick="ListTest_MouseDoubleClick" SelectionChanged="ListTest_SelectionChanged" IsSynchronizedWithCurrentItem="True" Grid.RowSpan="2">
<ListView.Resources>
<local:AddConverter x:Key="AddConverter"/>
</ListView.Resources>
<ListView.Effect>
<DropShadowEffect/>
</ListView.Effect>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="136" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Amount (€)" Width="80" DisplayMemberBinding="{Binding Sum}"/>
<GridViewColumn Header="Amount With Interest (€)" Width="80">
<GridViewColumn.DisplayMemberBinding>
<MultiBinding Converter="{StaticResource AddConverter}">
<Binding Path="Sum"/>
<Binding Path="interestRate_1"/>
</MultiBinding>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
您不一定需要值轉換器。您還可以公開一個計算屬性Cust并改為系結它。這取決于您的要求。
public class Cust
{
// ...your other code.
public decimal AmountWithInterest => sum interestRate_1;
}
<GridViewColumn Header="Amount With Interest (€)" Width="80" DisplayMemberBinding="{Binding AmountWithInterest}"/>
另請注意,您沒有實作INotifyPropertyChanged,這意味著您的屬性和計算屬性的更改不會反映在用戶界面中。
Apart from the errors you get, you should probably get familiar with the MVVM pattern.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436189.html
上一篇:自定義INotifyPropertyChanged行為LinqtoSQL
下一篇:動態資源的資料觸發校驗值
