I want to have DataGridCell
with text and image.
Currently, my code looks like that
XAML:
<DataGrid Name="myDataGrid" CellStyle="{StaticResource myCellStyle}" />
Style:
<Style x:Key="myCellStyle" TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"/>
<Image/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
C#:
myDataGrid.ItemsSource = myDataTable.DefaultView;
The question is:
How to bind text to a TextBlock
using ItemsSource
?
You must do couple of things to fix it
First, set 'AutoGenerateColumns' to true
<DataGrid CellStyle="{StaticResource myCellStyle}" AutoGenerateColumns="True">
Next in your cell style
<Style x:Key="myCellStyle" TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Text}"/>
<Image/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With