Reputation: 57
I'm trying to bind images in a ListView
via code behind. The goal is to write this section of code in C #. How does binding in code behind work?
This is my xaml code:
<ListView x:FieldModifier="public" x:Name="MyListView" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding ListViewDataList}">
<ListView.View>
<GridView x:Uid="GridViewTest">
<GridViewColumn Header="Column1" DisplayMemberBinding="{Binding Column1Text}"/>
<GridViewColumn Header="Column2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="50" Height="50" Source="{Binding Column2Img}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Column1Text and Column2Img are properties. I tried something like this:
GridView MyGridView = new GridView();
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("Column1Text");
gvc1.Header = "Column1";
MyGridView.Columns.Add(gvc1);
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Height = 50;
img.Width = 50;
img.Source = new Binding("Column2Img");
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
DataTemplate dt = new DataTemplate();
dt.VisualTree = spFactory;
GridViewColumn gvc2 = new GridViewColumn();
gvc2.Header = "Column2";
gvc2.CellTemplate = dt;
MyGridView.Columns.Add(gvc2);
MyListView.View = MyGridView;
But the binding for image in code behind doesn't work.
Upvotes: 1
Views: 569
Reputation: 169420
You should bind the Source
property instead of setting it to a Binding
object directly:
img.SetBinding(Image.SourceProperty, new Binding("Column2Img"));
But you also need to create an FrameworkElementFactory
for the Image
element that you add to spFactory
:
FrameworkElementFactory imgFactory = new FrameworkElementFactory(typeof(Image));
imgFactory.SetValue(Image.HeightProperty, 50.0);
imgFactory.SetValue(Image.WidthProperty, 50.0);
imgFactory.SetBinding(Image.SourceProperty, new Binding("Column2Img"));
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
spFactory.AppendChild(imgFactory);
Note that using a FrameworkElementFactory
is a deprecated way to programmatically create templates though: https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx.
The recommended way to programmatically create a template is to load XAML from a
string
or a memory stream using theLoad
method of theXamlReader
class.
Upvotes: 1