bandrewk
Topic Author
Posts: 3
Joined: 09 May 2014, 18:23

ListView Code-Behind (C++)

02 Jun 2014, 20:33

Hello,

I am having a hard time implementing a code-behind for a ListView using the native sdk. Currently I'm searching for the ListView inside the XAML using FindName():
lvServerBrowser = uiRoot->FindName<ListView>("lvServerBrowser");
and then add elements to it using a collection:
Noesis::Gui::Collection* collection = new Collection();

collection->Add("Item");
collection->Add("Item");
collection->Add("Item");

lvServerBrowser->SetItemsSource(collection);
This does work, however, it only adds items to the first column. Could you provide an example on how to code-behind a ListView with multiple columns using the native sdk and C++?

Thank you!
Last edited by bandrewk on 06 Jun 2014, 07:35, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 3013
Joined: 22 Dec 2011, 19:20

Re: ListView Code-Behind (C++)

04 Jun 2014, 11:45

Hi,

ListView columns are filled as specified by the xaml. Each GridViewColumn of the GridView determines which data member is binded to the column by setting the DisplayMemberBinding property.

In the following link you will find a simple tutorial for ListViews:

http://tech.pro/tutorial/742/wpf-tutori ... iew-part-1

I will implement in NoesisGUI the last example shown there. The xaml would look something like this:
<Grid x:Class="ListViewTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Height="216" Width="435">
  <StackPanel>
    <ListView ItemsSource="{Binding GameCollection}">
      <ListView.View>
        <GridView>
          <GridViewColumn Width="140" Header="Game Name" 
              DisplayMemberBinding="{Binding GameName}"  />
          <GridViewColumn Width="140" Header="Creator"  
              DisplayMemberBinding="{Binding Creator}" />
          <GridViewColumn Width="140" Header="Publisher" 
              DisplayMemberBinding="{Binding Publisher}" />
        </GridView>
      </ListView.View>
    </ListView>
    <Button HorizontalAlignment="Right" Margin="5,5,5,5" 
        Content="Add Row" Click="AddRow_Click" />
  </StackPanel>
</Grid>
Then we have to create the corresponding code-behind classes:
struct GameData : public Noesis::Core::BaseComponent
{
    NsString gameName;
    NsString creator;
    NsString publisher;

    GameData(const NsChar* n, const NsChar* c, const NsChar* p):
        gameName(n), creator(c), publisher(p)
    {
    }

    NS_REFLECTION_IMPLEMENT_INLINE(GameData, Noesis::Core::BaseComponent)
    {
        NsMeta<Noesis::Core::TypeId>("GameData");
        NsProp("GameData", &GameData::gameData);
        NsProp("Creator", &GameData::creator);
        NsProp("Publisher", &GameData::publisher);
    }
};
class ListViewTest : public Noesis::Gui::Grid
{
public:
    ListViewTest(): mGameCollection(*new Collection())
    {
        Ptr<GameData> gameData = *new GameData("World Of Warcraft", "Blizzard", "Blizzard");
        mGameCollection->Add(gameData.GetPtr());
        gameData = *new GameData("Halo", "Bungie", "Microsoft");
        mGameCollection->Add(gameData.GetPtr());
        gameData = *new GameData("Gears Of War", "Epic", "Microsoft");
        mGameCollection->Add(gameData.GetPtr());
    }
    
    Collection* GetGameCollection() const
    {
        return mGameCollection.GetPtr();
    }

private:
    void AddRow_Click(Noesis::Core::BaseComponent* sender, const RoutedEventArgs& e)
    {
        Ptr<GameData> gameData = *new GameData("A New Game", "A New Creator", "A New Publisher");
        mGameCollection->Add(gameData.GetPtr());
    }

private:
    Ptr<Noesis::Gui::Collection*> mGameCollection;

    NS_IMPLEMENT_REFLECTION_INLINE(ListViewTest, Noesis::Gui::Grid)
    {
        NsMeta<Noesis::Core::TypeId>("ListViewTest");
        NsProp("GameCollection", &ListViewTest::GetGameCollection);
        NsFunc("AddRow_Click", &ListViewTest::AddRow_Click);
    }
};
And register the ListViewTest class so it can be created by the component factory when xaml is loaded:
extern "C" NS_DLL_EXPORT
void NsRegisterReflection(Noesis::Core::ComponentFactory* factory, NsBool registerComponents)
{
    NS_REGISTER_COMPONENT(ListViewTest)
}
That's all ;)
 
bandrewk
Topic Author
Posts: 3
Joined: 09 May 2014, 18:23

Re: ListView Code-Behind (C++)

06 Jun 2014, 07:34

Thank you! :)

Who is online

Users browsing this forum: No registered users and 2 guests