user3085980
user3085980

Reputation: 1

how to List view with binding

I have a problem to binding with listview I attach code can please any can help with this,

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

why it's not working

Upvotes: 0

Views: 44

Answers (2)

Somedust
Somedust

Reputation: 1170

You are doing pretty much everything wrong. You should use properties instead of accessor methods in your ScriptStep class, if you want that data to be bindable. It isn't necessary to implement INotifyPropertyChanged, if you don't need an ability to change ScriptStep contents. There is also no need in setting ListView's ItemSource property explicitly in codebehind, because it is already set via binding. Myscript.ToString() statement is something so bizzare, that I don't event know how to comment it. I'm posting a very simplified example of how to achieve your goal.

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>

Codebehind:

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

Execution result (after clicking the button):

Result

Upvotes: 1

Nitin Purohit
Nitin Purohit

Reputation: 18578

You will need to implement INotifiyPropertyChanged to your ScriptStep class and create property over _step variable

public int Step
{
   get{return _step;}
   set
   {
     _set =value;
     RaiseProepertyChanged("Step");
   }
}

and update your binding as

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

Upvotes: 0

Related Questions