我在嘗試將標簽更改為串列框中選擇的當前專案時遇到問題。有沒有辦法在不創建串列的情況下做到這一點?原始選定專案加載得很好,但是當值更改或選定專案更改時,我無法更新標簽。我需要新鮮的眼睛。
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();
}
}
}
}
uj5u.com熱心網友回復:
你的代碼有點困惑。
里面有很多額外的'{''}'。
此外,評論與代碼不一致,請參閱下面的評論。
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}");
}
要考慮的另一件事是創建單個方法,您可以在其中多次執行相同的代碼。
IE
private func<lstStudentInfo,string[]> get_items = x => x.Split('|');
希望這能讓你走上正確的道路。
XAML 中也可能存在問題,希望除錯文本對您有所幫助。
讓我們添加更多靈感可能嗎?
聽起來您沒有正確地將視圖模型系結到視窗。
見下文。
視窗.xaml
創建要顯示的內容的基本布局。
Bind everything over to the data model, event the selected item.
Let the view model handle all and the window just look pretty.
Window.xaml.cs
Create an instance of your view model and assign it as the Data Context.
From here, you can do anything you like to the view model, and the window will be updated.
My_View_Model.cs
Here is where everything resides that is bound to the Window.xaml.
Notice when the Label_Text is set, it then calls the OnPropertyChanged method.
There are many ways to implement this, but basically they are all the same, all you are doing is invoking the PropertyChanged Event with the name of the object that needs to be refreshed in the window.
The PropertyChanged event goes off and tells the window to update.
Any questions, feel free to ask.
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();
}
}
}
uj5u.com熱心網友回復:
我得到了它的作業 - 謝謝大家
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://www.uj5u.com/qita/450320.html
