0

Ive looked about 50 or more webpages about this control. and it seems to me that it might be useless to me.

It seams that the "GridView" is a "View" of the "ListView" control. i dont have any issues in using the control and loading data but manipulating it seems to be difficult.

public partial class Designer : UserControl
{
    public Designer()
    {
        InitializeComponent();
        List<RandomData> NewItem = new List<RandomData>();
        NewItem.Add(new RandomData { Column1 = "Item1", Column2 = "MoreData", Column3 = "MoreData", Column4 = "MoreData" });
        ListView_1.ItemsSource = NewItem;
    }
}
public class RandomData
{
    public String Column1 { get; set; }
    public String Column2 { get; set; }
    public String Column3 { get; set; }
    public String Column4 { get; set; }
}

Simple enough, it loads the data into the columns. however what it i want to load other stuff in there. like a checkbox or a image file or something.

 public Designer()
        {
            InitializeComponent();
            CheckBox AttemptThis = new CheckBox();
            AttemptThis.Content = "Testing";
            AttemptThis.IsChecked = true;

            List<RandomData> NewItem = new List<RandomData>();
            NewItem.Add(new RandomData { Column1 = AttemptThis, Column2 = "MoreData", Column3 = "MoreData", Column4 = "MoreData" });
            ListView_1.ItemsSource = NewItem;
        }
    }
    public class RandomData
    {
        public CheckBox Column1 { get; set; }
        public String Column2 { get; set; }
        public String Column3 { get; set; }
        public String Column4 { get; set; }
    }

And i get the checkbox.tostring() appear in the column??

is this control going to be able to do this? Also is there a way to have a checkbox or image appear if the cell is a certian value ?

RacerNerd
  • 1,579
  • 1
  • 13
  • 31
New Bee
  • 991
  • 1
  • 13
  • 24
  • Your problem is not the `GridView`. Your problem is your approach. Where is your XAML? WPF is about XAML and DataBinding, not procedural winforms-like code – Federico Berasategui Jan 03 '14 at 04:20

1 Answers1

7

If you're working with WPF, you really need to forget any and all notions and approaches you might be used to from archaic technologies such as winforms and understand and embrace The WPF Mentality.

manipulating it seems to be difficult

YES. The WPF Visual Tree is a really complex structure with all sorts of arcane behavior that you really do not want to get into. That's why you must Learn MVVM before you ever write a single line of code in WPF

I want to load other stuff in there. like a checkbox or a image file or something.

You do not "load" stuff into the UI. You define the UI in XAML and use DataBinding to bind the UI to relevant data, defined in either a Data Model or a ViewModel:

<Window x:Class="WpfApplication7.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <ListView ItemsSource="{Binding}">
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}"
                                      Content="{Binding DisplayName}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>

Code Behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        DataContext = Enumerable.Range(0,10)
                                .Select(x => new DataItem()
                                {
                                    DisplayName = "Item" + x.ToString(),
                                    IsSelected = x % 2 == 0
                                });
    }
}

Data Item:

public class DataItem
{
    public bool IsSelected { get; set; }

    public string DisplayName { get; set; }
}

Result:

enter image description here

  • See how there is no need at all to manipulate any UI element in procedural code. You simply define the Data in the form of simple properties and then set the UI's DataContext to that.
  • Two-Way DataBinding will also save you the need to "read the values" back from the UI after they've been modified. There is no such thing as "read data from the UI" in WPF because DataBinding takes care of that. Simply read the data from your Data Items.
  • WPF Rocks. Copy and paste my code in a File -> New Project -> WPF Application and see the results for yourself.
Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Your right, i am used to more "archaic" programming languages. i have been working with using Styles and Setters to "Manipulate" the contents of the GridView, i can do this check box stuff and some triggers for mouse movement and onclick however how do i approch manipulating an cell/item based on its contents IE: My GridView lists all the computers on the subnet. it then attempts to connect to them to see if the user has access to the computer. if it doesnt i want to put a impage inplace to identify that – New Bee Jan 03 '14 at 05:41