如何使用绑定列出视图

时间:2013-12-10 08:23:51

标签: wpf

我遇到与listview绑定的问题我附加代码可以请任何可以帮助的,

 <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();
    }
}

为什么它不起作用

2 个答案:

答案 0 :(得分:1)

你几乎都做错了。如果希望数据可绑定,则应在ScriptStep类中使用属​​性而不是访问器方法。如果您不需要更改ScriptStep内容的功能,则无需实现INotifyPropertyChanged。在codebehind中也没有必要显式设置ListView的ItemSource属性,因为它已经通过绑定设置了。 Myscript.ToString()语句是如此古怪,我不知道如何评论它。我发布了一个如何实现目标的简单示例。

XAML:

<Window x:Class="WpfTestBench.ScriptEditor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Script editor" Width="300" SizeToContent="Height">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ListView Grid.Row="0" ItemsSource="{Binding Steps}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Step" DisplayMemberBinding="{Binding Step}"/>
                    <GridViewColumn Header="Cooking time" DisplayMemberBinding="{Binding CookingTime}"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Grid.Row="1" Content="Add random steps" Click="AddStepButton_OnClick" />
    </Grid>
</Window>

代码隐藏:

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; }
        }
    }
}

执行结果(点击按钮后):

Result

答案 1 :(得分:0)

您需要对INotifiyPropertyChanged课程实施ScriptStep并在_step变量

上创建属性
public int Step
{
   get{return _step;}
   set
   {
     _set =value;
     RaiseProepertyChanged("Step");
   }
}

并将您的绑定更新为

<GridViewColumn Header="Step"  DisplayMemberBinding="{Binding Step}"/>