我在试图使标签更改为列表框中选择的当前项目时遇到困难。有没有办法做到这一点而不创建一个列表?原始选定的项正在加载,但当值更改或所选项更改时,我无法使标签更新。我需要新的眼睛。
public frmStudentScores()
{
InitializeComponent();
}
//load list box with initial students and grades
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudentInfo.Items.Add("Joel Murach|97|91|83");
lstStudentInfo.Items.Add("Doug Lowe|99|93|97");
lstStudentInfo.Items.Add("Anne Boehm|100|100|100");
//set focus on first item in list
lstStudentInfo.SelectedIndex = 0;
//string to seperate items in list box by | to use for labels
string[] str = lstStudentInfo.SelectedItem.ToString().Split('|');
int total = 0;
for (int i = 1; i < str.Length; i++)
{
total += int.Parse(str[i]);
}
//total of all scores in selected row
lblTotal.Text = total.ToString();
//count of the scores in the selected row
lblCount.Text = (str.Length - 1).ToString();
//total divided by the count of the scores in selected row
lblAverage.Text = (total / (str.Length - 1)).ToString();
}
private void lstStudentInfo_SelectedIndexChanged(object sender, EventArgs e)
{
//if the index is changed in the list box
string current = lstStudentInfo.SelectedItem.ToString();
if (lstStudentInfo.SelectedIndex >= 0)
{
string[] str = lstStudentInfo.SelectedItem.ToString().Split('|');
int total = 0;
{
for (int i = 0; i < str.Length; i++)
{
total += int.Parse(str[i]);
}
{
lblTotal.Text = lstStudentInfo.SelectedItem.ToString();
lblCount.Text = (str.Length - 1).ToString();
lblAverage.Text = (total / (str.Length - 1)).ToString();
}
}
}
}发布于 2022-03-23 17:00:40
被你的代码弄糊涂了。
里面有很多额外的'{‘}’。
此外,注释与代码不一致,请参见下面的注释。
private void lstStudentInfo_SelectedIndexChanged(object sender, EventArgs e)
{
//if the index is changed in the list box
string current = lstStudentInfo.SelectedItem.ToString();
#region IF STATEMENT
//Do you want it to only update if changed?
//If so, you need to keep track of the index
if (lstStudentInfo.SelectedIndex >= 0)
{
string[] str = lstStudentInfo.SelectedItem.ToString().Split('|');
int total = 0;
#region UNKNOWN REGION
{
for (int i = 0; i < str.Length; i++)
{
total += int.Parse(str[i]);
}
#region UNKNOWN REGION
{
lblTotal.Text = lstStudentInfo.SelectedItem.ToString();
lblCount.Text = (str.Length - 1).ToString();
lblAverage.Text = (total / (str.Length - 1)).ToString();
}
#endregion
}
}
#endregion
#endregion
}我清理了一下,增加了一些错误处理和调试。
private void lstStudentInfo_SelectedIndexChanged(object sender, EventArgs e)
{
string current = string.empty;
string lblCount = string.empty;
string lblAverage = string.empty;
string[] str = null;
int total = 0;
int valCount = 0;
int valAvg = 0;
//added some error handling
try{
if (lstStudentInfo.SelectedIndex >= 0)
{
//moved inside if statement, avoid errors if nothing is selected;
current = lstStudentInfo.SelectedItem.ToString();
str = current.Split('|');
for (int i = 0; i < str.Length; i++)
{
total += int.Parse(str[i]);
}
valCount = (str.Length - 1);
lblCount = valCount.ToString();
valAvg = total/valCount;
lblAverage = valAvg.ToString();
}
}
catch(Exception e){
lblCount = "####";
lblTotal = "####";
current = "ERROR";
}
//moved outside if statment, updates labels when nothing is selected
lblTotal.Text = current;
lblCount.Text = lblCount;
lblAverage.Text = lblAverage;
//added debugging output
Debug.WriteLine($"Student Info Selection Changed: Count: {valCount} Avg: {valAvg}");
}要考虑的另一件事是创建单个方法,在这些方法中要多次执行相同的代码。
即
private func<lstStudentInfo,string[]> get_items = x => x.Split('|');希望这能让你走上正确的道路。
XAML中也可能出现问题,希望调试文本能够帮助您。
让我们多加一点灵感吧?
听起来您的视图模型没有正确地绑定到窗口。
见下文。
Window.xaml
创建要显示的内容的基本布局。
将所有内容绑定到数据模型,将所选项事件处理。
让视图模型处理所有的东西,而窗口看起来很漂亮。
Window.xaml.cs
创建视图模型的实例,并将其指定为数据上下文。
在这里,您可以对视图模型做任何您想做的事情,并且窗口将被更新。
My_View_Model.cs
这里是绑定到Window.xaml的所有东西的位置。
注意,当设置Label_Text时,它将调用OnPropertyChanged方法。
实现这一点的方法有很多种,但基本上它们都是相同的,您所做的就是使用需要在窗口中刷新的对象的名称调用PropertyChanged事件。
PropertyChanged事件停止,并通知窗口进行更新。
任何问题,请随便问。
Window.xaml:
<Window x:Class="WPF_Label_Update.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:WPF_Label_Update"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="The_WPF_Grid">
<Grid.DataContext>
<local:My_View_Model/>
</Grid.DataContext>
<Grid.Resources>
<DataTemplate x:Key="myDataTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=obj_name}" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<StackPanel Orientation="Horizontal">
<ListBox x:Name="list_choice"
SelectedItem="{Binding My_Selected_Object}"
SelectionMode="Single"
ItemsSource="{Binding My_Objects}"
ItemTemplate="{StaticResource myDataTemplate}">
</ListBox>
<StackPanel Orientation="Vertical">
<Label x:Name="lbl_output_0" Content="{Binding Label_Text_0}"/>
<Label x:Name="lbl_output_1" Content="{Binding Label_Text_1}"/>
<Label x:Name="lbl_output_2" Content="{Binding Label_Text_2}"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_Label_Update
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
My_View_Model myViewModel;
private void generateData()
{
StringBuilder _text = new StringBuilder() ;
for (int i = 0; i < 10; i++)
{
_text.Clear();
_text.Append($"Number {i}");
for (int ii = 0; ii < i; ii++)
{
_text.Append($"|{ii}");
}
myViewModel.Add_Item(new my_obj(_text.ToString()));
}
}
public MainWindow()
{
InitializeComponent();
myViewModel =
new My_View_Model();
this.The_WPF_Grid.DataContext = myViewModel;
generateData();
}
private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Something Was Selected");
}
}
}My_View_Model.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace WPF_Label_Update
{
public class My_View_Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Label_Text_0 { get { return lbl0; } set { lbl0 = value; OnPropertyChanged(); } }
private string lbl0 = string.Empty;
public string Label_Text_1 { get { return lbl1; } set { lbl1 = value; OnPropertyChanged(); } }
private string lbl1 = string.Empty;
public string Label_Text_2 { get { return lbl2; } set { lbl2 = value; OnPropertyChanged(); } }
private string lbl2 = string.Empty;
public my_obj My_Selected_Object {
get {
return mySelectedObj;
}
set {
mySelectedObj = value;
on_mySelectedObjChange();
} }
private my_obj mySelectedObj = null;
/// <summary>
/// Update lables when something changes.
/// </summary>
private void on_mySelectedObjChange()
{
switch (mySelectedObj == null)
{
case true:
Label_Text_0 = string.Empty;
Label_Text_1 = string.Empty;
Label_Text_2 = string.Empty;
break;
default:
Label_Text_0 = mySelectedObj.obj_text;
Label_Text_1 = mySelectedObj.obj_name;
Label_Text_2 = mySelectedObj.obj_count.ToString();
break;
}
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public ObservableCollection<my_obj> My_Objects { get { return my_objs; } }
private readonly ObservableCollection<my_obj> my_objs = new ObservableCollection<my_obj>();
public void Add_Item(my_obj new_obj)
{
my_objs.Add(new_obj);
}
public My_View_Model()
{
}
}
public class my_obj
{
public string obj_text { get; set; }
public string obj_name { get { return obj_text.Split('|')[0]; } }
public int obj_count { get { return obj_text.Split('|').Length; } }
public my_obj(string inputText)
{
this.obj_text = inputText.Trim();
}
}
}发布于 2022-03-24 22:25:54
我把它处理好了-谢谢大家
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstStudents.SelectedIndex != -1)
{
string student =
(string)studentScores[lstStudents.SelectedIndex];
string[] scores = student.Split('|');
int total = 0;
for (int i = 1; i < scores.Length; i++)
total += Convert.ToInt32(scores[i]);
int count = scores.Length - 1;
int average = 0;
if (total > 0)
average = total / count;
lblScoreTotal.Text = total.ToString();
lblScoreCount.Text = count.ToString();
lblAverage.Text = average.ToString();
}
}https://stackoverflow.com/questions/71590696
复制相似问题