从列表框中保留更新的文本框中的值。 [英] Present value from listbox in a textbox that keep update.
问题描述
大家好!
我在我的WPF应用程序中陷入困境。
我需要创建一个.cs类( Configuration.cs)具有三个属性(Name,PropA和PropB)。
在我的UI中,我想显示我在listBox中的配置实例中的所有名称。
当我选择一个项目时在listBox中,属性PropA和PropB必须显示在两个不同的TextBox中。此外,我希望这些textBoxes是可编辑的,当用户更改值并使用按钮提交更改时,相应的Configuration实例会更新。
我该怎么办?这个?我的Configuartion.cs课必须如何?我如何在这个类中定义属性?
感谢大家。
这里是你的配置类..
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
命名空间 Dispacher
{
public class 配置
{
public String 名称{获取; set ; }
public string PropA {获得跨度>; set ; }
public string PropB {获得跨度>; set ; }
}
}
这是你的ViewModel
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Collections.ObjectModel;
使用 System.Windows.Input;
使用 System.Windows;
使用 System.Diagnostics;
使用 System.ComponentModel;
命名空间 Dispacher
{
public class DisplayViewModel
{
public ObservableCollection< configurations> SelectedPeople { get ; private set ; }
public 列表<配置> AvailablePeople { get ; private set ; }
public DelegateCommand Submitcommand { get ; private set ; }
public string OutPut {获得跨度>; set ; }
public DisplayViewModel()
{
// 将为视图生成数据的构造函数
AvailablePeople = new 列表<配置> {
new 配置{Name = Basavraj ,PropA = John,PropB = Doe},
new 配置{Name = Mallikarjun,PropA = Michael,PropB = Jones},
new 配置{Name = Swami,PropA = 简,PropB = Smith},};
Submitcommand = new DelegateCommand(modifiedData);
}
// 在这里你可以获得修改后的数据对象
// 放置断点以查看修改后的数据
private void modifiedData()
{
Predicate< configurations> pred = item = > item.PropA!= 约翰 || item.PropA!= Michael || item.PropA!= Jane;
// 修改后的数据
var modifieddata = AvailablePeople.Find(pred);
if (modifieddata!= null )
OutPut = < span class =code-string> PropA:= + modifieddata.PropA + ,PropB:= + modifieddata.PropB;
}
public class DelegateCommand:ICommand
{
private Action _executeMethod;
public DelegateCommand(Action executeMethod)
{
_executeMethod = executeMethod;
}
public bool CanExecute(< span class =code-keyword> object 参数)
{
return 真跨度>;
}
public event EventHandler CanExecuteChanged;
public void 执行( object 参数)
{
_executeMethod.Invoke();
}
}
}
}
< / < span class =code-leadattribute>配置 > < / 配置 > < / 配置 > < / 配置 >
这是您的观点
< window x:class = Dispacher.Window1 <温泉n class =code-attribute> xmlns:x = #unknown < span class =code-keyword>>
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http ://schemas.microsoft.com/winfx/2006/xaml
xmlns:Sample =clr-namespace:Dispacher
Title =Window1Height =500Width =500> ;
< window.resources >
< objectdataprovider >
x:Key =Viewmodel
ObjectType ={x:Type Sample:DisplayViewModel}/>
< / objectdataprovider > < / window.resources >
< grid >
< dockpanel datacontext = {StaticResource Viewmodel } >
< listbox 名称 = list dockpanel.dock = 左 width = 130 >
ItemsSource ={Binding AvailablePeople}
DisplayMemberPath =Name
SelectedValuePath =ListBox/>
< textbox < span class =code-attribute> text = {Binding ElementName = list,Path = SelectedItem。 PropA} height = 50 width = 100 / >
< textbox text = {Binding ElementName = list,Path = SelectedIt em.PropB} height = 50 width < span class =code-keyword> = 100 / >
< 按钮 width = 20 height = 50 >
Command ={Binding Submitcommand}
CommandParameter ={Binding AvailablePeo ple,ElementName = list}
Content =>>/>
< / button > < / listbox > < / dockpanel >
< / grid >
< / window >
如果得到解决方案请标记已接受
我不了解有关属性的第一部分,但关于第二部分要更改您可以使用的列表框的值:
int a = ListBoxName.SelectedIndex;
获取所选项目的索引然后你应该通过以下方式获取数组中的项目:
string [] array;
int i = 0 ;
int b = ListBoxName.Items.Count;
while (i < b)
{
array [ i] = ListBoxName.Items [i];
i ++;
}
然后用文本框编辑您考虑的项目作为输入值:
< pre lang =c#> array [a] = textBox.Text;
最后版本完成后你应该从数组中取回新的列表框值:
i = 0 ;
while (i > b);
{
ListBoxName.Items.Add(array [i]);
i ++;
}
已经完成。
i希望能帮助你解决第一部分,但你应该解释一下。
Doostl
Hi to all!!
I´m mired in my WPF app.
I need to create a .cs class (Configuration.cs) with three properties (Name,PropA and PropB).
In my UI I want to show all the Names in the Configuration instances that I had in a listBox.
When I select an Item in the listBox, the properties PropA and PropB must shown in two different TextBoxes. Furthermore, I want these textBoxes are editable and when the user changes the value and submit the changes with a button, the appropriate Configuration instance is updated.
How can I do this? How must my Configuartion.cs class be? How I have to define the properties in this class?
Thanks for all.
here is your configurations class..
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dispacher { public class Configurations { public String Name { get; set; } public string PropA { get; set; } public string PropB { get; set; } } }
Here is your ViewModel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Windows.Input; using System.Windows; using System.Diagnostics; using System.ComponentModel; namespace Dispacher { public class DisplayViewModel { public ObservableCollection<configurations> SelectedPeople { get; private set; } public List<configurations> AvailablePeople { get; private set; } public DelegateCommand Submitcommand { get; private set; } public string OutPut { get; set; } public DisplayViewModel() { // Constructor which will generate data for view AvailablePeople = new List<configurations> { new Configurations { Name="Basavraj", PropA = "John", PropB = "Doe" }, new Configurations {Name="Mallikarjun", PropA = "Michael", PropB = "Jones" }, new Configurations {Name="Swami", PropA = "Jane", PropB = "Smith" }, }; Submitcommand = new DelegateCommand(modifiedData); } //here you can get modified data object //Put breakpoint to see modified data private void modifiedData() { Predicate<configurations> pred = item => item.PropA != "John" || item.PropA != "Michael" || item.PropA != "Jane"; //modified data var modifieddata = AvailablePeople.Find(pred); if (modifieddata != null) OutPut = "PropA :=" + modifieddata.PropA + ",PropB :=" + modifieddata.PropB; } public class DelegateCommand : ICommand { private Action _executeMethod; public DelegateCommand(Action executeMethod) { _executeMethod = executeMethod; } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _executeMethod.Invoke(); } } } } </configurations></configurations></configurations></configurations>
here is your view
<window x:class="Dispacher.Window1" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Sample="clr-namespace:Dispacher" Title="Window1" Height="500" Width="500"> <window.resources> <objectdataprovider> x:Key="Viewmodel" ObjectType="{x:Type Sample:DisplayViewModel}"/> </objectdataprovider></window.resources> <grid> <dockpanel datacontext="{StaticResource Viewmodel}"> <listbox name="list" dockpanel.dock="Left" width="130"> ItemsSource="{Binding AvailablePeople}" DisplayMemberPath="Name" SelectedValuePath="ListBox"/> <textbox text="{Binding ElementName=list, Path=SelectedItem.PropA}" height="50" width="100" /> <textbox text="{Binding ElementName=list, Path=SelectedItem.PropB}" height="50" width="100" /> <button width="20" height="50"> Command="{Binding Submitcommand}" CommandParameter="{Binding AvailablePeople, ElementName=list}" Content=">>"/> </button></listbox></dockpanel> </grid> </window>
Please mark accepted if got solution
I didn't understand the first part about properties but about the second part to change the value of a list box you can use:
int a = ListBoxName.SelectedIndex;
to get the index of the selected item then you should get the items in an array by:
string[] array; int i = 0; int b = ListBoxName.Items.Count; while (i < b) { array[i] = ListBoxName.Items[i]; i++; }
then edit your considered item by a text box as entered value:
array[a] = textBox.Text;
at last the edition is completed and you should get back the new list box value from array:
i = 0; while (i > b); { ListBoxName.Items.Add(array[i]); i++; }
and it's done.
i hope so to help you about first part but you should explain more.
Doostl
这篇关于从列表框中保留更新的文本框中的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!