0

I have a short question about WPF and C#. Can somebody give me an example how the following xaml code would look in C# code?

In xaml this works, but i would like to do some dynamic C# creation and need to do this in C# code as well. I am able to do every bit of binding in code, except this part..

<GridViewColumn>
    <GridViewColumn.CellTemplate>
         <DataTemplate>
              <Image Source="{Binding FileImage, Converter={StaticResource imageConverter}}" Width="32" Height="32"/>
         </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

The way i load my other columns is like this:

leftColumnOne = new GridViewColumn();
leftColumnOne.Header = "First name";
leftColumnOne.DisplayMemberBinding = new Binding("FirstName");
stackr
  • 2,742
  • 27
  • 44
  • Try it http://social.msdn.microsoft.com/Forums/vstudio/en-US/cbb6ae99-6bb5-46e7-9bb5-4374ce564c6f/create-a-datatemplate-by-code-and-add-it-to-a-listview?forum=wpf – progpow Dec 04 '13 at 14:19
  • Check this [out](http://stackoverflow.com/questions/248362/how-do-i-build-a-datatemplate-in-c-sharp-code). – Rohit Vats Dec 04 '13 at 14:47
  • **Don't create or manipulate UI elements in procedural code in WPF. That's what XAML is for.** - There's no need to "dynamically define the columns in a ListView" - How many different combinations of columns do you really have? - You should be using `DataTemplate`s to provide different Views for different data sets, not using a horrible winforms-like procedural approach. – Federico Berasategui Dec 04 '13 at 14:58
  • Unfortunately i have an exception on this part, this must be dynamic. the xaml is to static for this case, because i'm using a WPF window with a page inside it that needs to update the gui dynamically. And the two examples work fine with TextBlocks, but my Image needs to be converted. Therefore i need to know how to use this code including a IValueConverter. – stackr Dec 04 '13 at 15:17
  • @stackr No XAML is not static. You have no idea what you're talking about. Please learn WPF properly and don't try to use it as if it were winforms. There are many ways to achieve a dynamic column behavior in a ListView, however my question if you really need this remains unanswered. How many different combinations of columns will there be? – Federico Berasategui Dec 04 '13 at 15:27
  • It is unknown howmany combinations there will be. Currently i'm working with 3 or 4 different combinations, but they will grow fast. – stackr Dec 04 '13 at 15:34
  • [here](http://stackoverflow.com/a/13824607/643085) is one example by Rachel on how to do that with a Nested ItemsControl. Another way would be to use `DataTemplate`s to dynamically change the View depending on the data shown. a crappy dinosaur procedural winforms-like code behind approach is definitely not the solution for this. – Federico Berasategui Dec 04 '13 at 15:36

0 Answers0