Wpf 如何使用绑定列出视图
我有一个与listview绑定的问题,我附上代码,请任何人都可以帮忙Wpf 如何使用绑定列出视图,wpf,Wpf,我有一个与listview绑定的问题,我附上代码,请任何人都可以帮忙 <ListView Name="ScriptSteplist" ItemsSource="{Binding MyScript}" Grid.Row="1" > <ListView.View> <GridView> <GridViewColumn Header="Step" DisplayMemberBindin
<ListView Name="ScriptSteplist" ItemsSource="{Binding MyScript}" Grid.Row="1" >
<ListView.View>
<GridView>
<GridViewColumn Header="Step" DisplayMemberBinding="{Binding step}"/>
</GridView>
</ListView.View>
</ListView>
public class ScriptStep
{
private int _step;
private string _Cookingtime;
private int _KJ_limit;
private string _CookingMode;
private string _MaxPower;
private string _RD_Color;
private string _ConvectionMode;
private int _Temp;
private string _ConvectionColor;
private int _flip;
public ScriptStep()
{
_step = 0;
_Cookingtime = "00:00:00";
_KJ_limit = 0;
_CookingMode = "Heat";
_MaxPower = "0";
_RD_Color = "";
_ConvectionMode = "";
_Temp = 0;
_ConvectionColor = "";
_flip = 0;
}
#region get/set
public int GetStep()
{
return _step;
}
public void SetStep(int stepNumber)
{
_step = stepNumber;
}
public string GetCookingTime()
{
return _Cookingtime;
}
public void SetCookingTime(string CookingTime)
{
_Cookingtime = CookingTime;
}
public int GetKJLimit()
{
return _KJ_limit;
}
public void SetKJLimit(int KJLimit)
{
_KJ_limit = KJLimit;
}
public string GetCookingMode()
{
return _ConvectionMode;
}
public void SetCookingMode(string cookMode)
{
_ConvectionMode = cookMode;
}
public string GetMaxPower()
{
return _MaxPower;
}
public void SetMaxPower(string MaxPower)
{
_MaxPower = MaxPower;
}
public string GettRFColor()
{
return _RD_Color;
}
public void SetRFColor(string ColorName)
{
_RD_Color = ColorName;
}
public string GetConvectionMode()
{
return _ConvectionMode;
}
public void SetConvectionMode(string ConvectionMode)
{
_ConvectionMode = ConvectionMode;
}
public int GetTemp()
{
return _Temp;
}
public void setTemp(int temp)
{
_Temp = temp;
}
public string GetConvectionColor()
{
return _ConvectionColor;
}
public void SetConvectionColor(string ColorName)
{
_ConvectionColor=ColorName;
}
public int GetFliptime()
{
return _flip;
}
public void SetFlip(int flip)
{
_flip = flip;
}
#endregion
}
public partial class ScriptEditor : Window
{
ScriptStep _scriptStep;
public ObservableCollection<ScriptStep> Usersteps;
//public List<ScriptStep> UserStep;
ObservableCollection<ScriptStep> Myscript
{
get { return Usersteps; }
}
public ScriptEditor()
{
InitializeComponent();
_scriptStep = new ScriptStep();
Usersteps = new ObservableCollection<ScriptStep>();
this.DataContext = this;
}
private void AddStep_Click(object sender, RoutedEventArgs e)
{
_scriptStep.SetStep(Convert.ToInt32(steptextbox.Text));
_scriptStep.SetCookingTime(Timelimittextbox.Text);
_scriptStep.SetKJLimit(Convert.ToInt32(KJlimit.Text));
_scriptStep.SetCookingMode("Heat");
_scriptStep.SetMaxPower("500");
_scriptStep.SetRFColor("red");
_scriptStep.SetConvectionMode("SupHeat");
_scriptStep.setTemp(Convert.ToInt32(temptextbox.Text));
_scriptStep.SetConvectionColor("Black");
_scriptStep.SetFlip(45);
Myscript.Add(_scriptStep);
ScriptSteplist.ItemsSource= Myscript.ToString();
}
}
公共类脚本步骤
{
私人内部步骤;
私有字符串\u Cookingtime;
私人积分上限;
私有字符串\u CookingMode;
私有字符串_MaxPower;
私有字符串_RD_颜色;
私有字符串_对流模式;
私人内部温度;
私有字符串_conversionColor;
私人内部翻转;
公共脚本步骤()
{
_步长=0;
_Cookingtime=“00:00:00”;
_KJ_极限=0;
_烹饪模式=“加热”;
_MaxPower=“0”;
_RD_Color=“”;
_对流模式=”;
_温度=0;
_对流颜色=”;
_翻转=0;
}
#区域获取/设置
公共int GetStep()
{
返回步;
}
公共无效设置步骤(int步骤编号)
{
_步骤=步骤编号;
}
公共字符串GetCookingTime()
{
返回烹饪时间;
}
公共无效设置CookingTime(字符串CookingTime)
{
_烹调时间=烹调时间;
}
public int GetKJLimit()
{
返回_KJ_限制;
}
公共无效设置KJLimit(int KJLimit)
{
_KJ_极限=KJLimit;
}
公共字符串GetCookingMode()
{
返回对流模式;
}
公共无效设置CookingMode(字符串CookingMode)
{
_对流模式=烹饪模式;
}
公共字符串GetMaxPower()
{
返回最大功率;
}
公共无效SetMaxPower(字符串MaxPower)
{
_最大功率=最大功率;
}
公共字符串GettRFColor()
{
返回颜色;
}
public void SetRFColor(字符串ColorName)
{
_RD_Color=颜色名称;
}
公共字符串getConverctionMode()
{
返回对流模式;
}
public void setConverctionMode(字符串ConverctionMode)
{
_对流模式=对流模式;
}
公共int GetTemp()
{
返回温度;
}
公共void settmp(int-temp)
{
_温度=温度;
}
公共字符串getConverctionColor()
{
返回对流颜色;
}
public void setConverctionColor(字符串ColorName)
{
_对流颜色=颜色名称;
}
public int GetFliptime()
{
返回翻转;
}
公共void SetFlip(int flip)
{
_翻转=翻转;
}
#端区
}
公共部分类脚本编辑器:窗口
{
脚本步骤_ScriptStep;
公共可观察收集用户步骤;
//公共列表用户步骤;
ObservableCollection Myscript
{
获取{return Usersteps;}
}
公共脚本编辑器()
{
初始化组件();
_scriptStep=新建scriptStep();
Usersteps=newobserveCollection();
this.DataContext=this;
}
私有void AddStep_单击(对象发送方,路由目标)
{
_scriptStep.SetStep(Convert.ToInt32(steptextbox.Text));
_scriptStep.SetCookingTime(Timelimittextbox.Text);
_SetKJLimit(Convert.ToInt32(KJlimit.Text));
_脚本步骤。设置烹饪模式(“加热”);
_scriptStep.SetMaxPower(“500”);
_scriptStep.SetRFColor(“红色”);
_scriptStep.setConverctionMode(“SupHeat”);
_settep(Convert.ToInt32(testextbox.Text));
_scriptStep.setConverctionColor(“黑色”);
_scriptStep.SetFlip(45);
添加(_scriptStep);
ScriptSteplist.ItemsSource=Myscript.ToString();
}
}
为什么它不起作用您需要对
ScriptStep
类实现INotifiyPropertyChanged
,并在\u step
变量上创建属性
public int Step
{
get{return _step;}
set
{
_set =value;
RaiseProepertyChanged("Step");
}
}
并将您的绑定更新为
<GridViewColumn Header="Step" DisplayMemberBinding="{Binding Step}"/>
你几乎每件事都做错了。如果希望数据可绑定,则应在ScriptStep类中使用属性而不是访问器方法。如果您不需要更改ScriptStep内容,则无需实现INotifyPropertyChanged。也不需要在codebehind中显式设置ListView的ItemSource属性,因为它已经通过绑定进行了设置。ToString()语句非常奇怪,我不知道如何注释它。我正在发布一个非常简单的例子,说明如何实现你的目标 XAML:
代码隐藏:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfTestBench
{
public partial class ScriptEditor
{
private readonly ObservableCollection<ScriptStep> _steps = new ObservableCollection<ScriptStep>();
public ScriptEditor()
{
InitializeComponent();
Steps.Add(new ScriptStep(1, "00:01"));
Steps.Add(new ScriptStep(2, "00:05"));
Steps.Add(new ScriptStep(3, "00:02"));
DataContext = this;
}
public ObservableCollection<ScriptStep> Steps
{
get { return _steps; }
}
private void AddStepButton_OnClick(object sender, RoutedEventArgs e)
{
Steps.Add(new ScriptStep(4, "00:07"));
Steps.Add(new ScriptStep(5, "00:03"));
}
public class ScriptStep
{
public ScriptStep(int step, string time)
{
Step = step;
CookingTime = time;
}
public int Step { get; set; }
public string CookingTime { get; set; }
}
}
}
使用System.Collections.ObjectModel;
使用System.Windows;
命名空间WpfTestBench
{
公共部分类脚本编辑器
{
私有只读ObservableCollection _steps=新ObservableCollection();
公共脚本编辑器()
{
初始化组件();
步骤。添加(新脚本步骤(1,“00:01”);
步骤。添加(新脚本步骤(2,“00:05”);
步骤。添加(新脚本步骤(3,“00:02”);
DataContext=this;
}
公开收集步骤
{
获取{return\u steps;}
}
private void AddStepButton_OnClick(对象发送方,路由目标)
{
步骤。添加(新脚本步骤(4,“00:07”);
步骤。添加(新脚本步骤(5,“00:03”);
}
公共类脚本步骤
{
公共脚本步骤(int步骤,字符串时间)
{
步骤=步骤;
烹饪时间=时间;
}
公共int步骤{get;set;}
公共字符串CookingTime{get;set;}
}
}
}
执行结果(点击按钮后):
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfTestBench
{
public partial class ScriptEditor
{
private readonly ObservableCollection<ScriptStep> _steps = new ObservableCollection<ScriptStep>();
public ScriptEditor()
{
InitializeComponent();
Steps.Add(new ScriptStep(1, "00:01"));
Steps.Add(new ScriptStep(2, "00:05"));
Steps.Add(new ScriptStep(3, "00:02"));
DataContext = this;
}
public ObservableCollection<ScriptStep> Steps
{
get { return _steps; }
}
private void AddStepButton_OnClick(object sender, RoutedEventArgs e)
{
Steps.Add(new ScriptStep(4, "00:07"));
Steps.Add(new ScriptStep(5, "00:03"));
}
public class ScriptStep
{
public ScriptStep(int step, string time)
{
Step = step;
CookingTime = time;
}
public int Step { get; set; }
public string CookingTime { get; set; }
}
}
}