How to bind TextBlock.Text in DataGridCell template using ItemsSource
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
?
c# wpf xaml datagrid
add a comment |
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
?
c# wpf xaml datagrid
Who own'sItemsSource
? Where does it reside?
– ΩmegaMan
Nov 13 '18 at 21:37
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
Have you tried<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?
– Simon Evans
Nov 13 '18 at 22:48
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57
add a comment |
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
?
c# wpf xaml datagrid
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
?
c# wpf xaml datagrid
c# wpf xaml datagrid
edited Nov 13 '18 at 21:43
Wojtman
asked Nov 13 '18 at 21:21
WojtmanWojtman
768
768
Who own'sItemsSource
? Where does it reside?
– ΩmegaMan
Nov 13 '18 at 21:37
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
Have you tried<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?
– Simon Evans
Nov 13 '18 at 22:48
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57
add a comment |
Who own'sItemsSource
? Where does it reside?
– ΩmegaMan
Nov 13 '18 at 21:37
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
Have you tried<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?
– Simon Evans
Nov 13 '18 at 22:48
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57
Who own's
ItemsSource
? Where does it reside?– ΩmegaMan
Nov 13 '18 at 21:37
Who own's
ItemsSource
? Where does it reside?– ΩmegaMan
Nov 13 '18 at 21:37
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
Have you tried
<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?– Simon Evans
Nov 13 '18 at 22:48
Have you tried
<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?– Simon Evans
Nov 13 '18 at 22:48
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57
add a comment |
2 Answers
2
active
oldest
votes
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.
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
add a comment |
This assumes that myDataTable.DefaultView
is a list of some objects(class instances).
So when the grid gets its ItemsSource
set, it will then will present each row with one of the items from the list. So the binding to be specified will be a property/properties on the class.
So taking from your example template, if it is bound to a hypothetical list of person classes with FirstName
and LastName
on the class instance, one could set the template to bind to use those properties for each row like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding FirstName"/>
<TextBlock Text="Binding LastName"/>
<Image/>
</StackPanel>
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the newList<DTOClass
>.
– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289675%2fhow-to-bind-textblock-text-in-datagridcell-template-using-itemssource%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
add a comment |
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.
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
add a comment |
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.
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.
answered Nov 14 '18 at 0:43
RajNRajN
3,66521021
3,66521021
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
add a comment |
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
1
1
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
That is exactly what I needed. Thank you very much!
– Wojtman
Nov 14 '18 at 5:32
add a comment |
This assumes that myDataTable.DefaultView
is a list of some objects(class instances).
So when the grid gets its ItemsSource
set, it will then will present each row with one of the items from the list. So the binding to be specified will be a property/properties on the class.
So taking from your example template, if it is bound to a hypothetical list of person classes with FirstName
and LastName
on the class instance, one could set the template to bind to use those properties for each row like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding FirstName"/>
<TextBlock Text="Binding LastName"/>
<Image/>
</StackPanel>
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the newList<DTOClass
>.
– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
add a comment |
This assumes that myDataTable.DefaultView
is a list of some objects(class instances).
So when the grid gets its ItemsSource
set, it will then will present each row with one of the items from the list. So the binding to be specified will be a property/properties on the class.
So taking from your example template, if it is bound to a hypothetical list of person classes with FirstName
and LastName
on the class instance, one could set the template to bind to use those properties for each row like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding FirstName"/>
<TextBlock Text="Binding LastName"/>
<Image/>
</StackPanel>
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the newList<DTOClass
>.
– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
add a comment |
This assumes that myDataTable.DefaultView
is a list of some objects(class instances).
So when the grid gets its ItemsSource
set, it will then will present each row with one of the items from the list. So the binding to be specified will be a property/properties on the class.
So taking from your example template, if it is bound to a hypothetical list of person classes with FirstName
and LastName
on the class instance, one could set the template to bind to use those properties for each row like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding FirstName"/>
<TextBlock Text="Binding LastName"/>
<Image/>
</StackPanel>
This assumes that myDataTable.DefaultView
is a list of some objects(class instances).
So when the grid gets its ItemsSource
set, it will then will present each row with one of the items from the list. So the binding to be specified will be a property/properties on the class.
So taking from your example template, if it is bound to a hypothetical list of person classes with FirstName
and LastName
on the class instance, one could set the template to bind to use those properties for each row like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding FirstName"/>
<TextBlock Text="Binding LastName"/>
<Image/>
</StackPanel>
answered Nov 13 '18 at 22:58
ΩmegaManΩmegaMan
16.1k54260
16.1k54260
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the newList<DTOClass
>.
– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
add a comment |
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the newList<DTOClass
>.
– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Unfortunatelly, myDataTable is made from strings (not custom objects with properties) and that's the problem. I have no idea how bind single string to my TextBlock.
– Wojtman
Nov 13 '18 at 23:02
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the new
List<DTOClass
>.– ΩmegaMan
Nov 13 '18 at 23:03
Create a Data Transfer Object class (DTO) with the appropriate properties and take the datatable data and new up a list of the DTOs from it. Program the DTO class's constructor to get specific columns from the datatable and load target properties from that. Then bind as shown but have the grid use the new
List<DTOClass
>.– ΩmegaMan
Nov 13 '18 at 23:03
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
I made as u said and now if style is implemented then grid do not show data but if style is not implemented then every cell have value like "myNamespace.myClass". So maybe there is something wrong with this property in my style <Setter Property="Template"> ?
– Wojtman
Nov 13 '18 at 23:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289675%2fhow-to-bind-textblock-text-in-datagridcell-template-using-itemssource%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Who own's
ItemsSource
? Where does it reside?– ΩmegaMan
Nov 13 '18 at 21:37
My mistake. I forgot about DataGrid name property which is set in my code. Please take a look on my example now.
– Wojtman
Nov 13 '18 at 21:43
Have you tried
<TextBlock Text="Binding ColumnName"/>
with ColumnName being the text column from your DataTable that you want to display?– Simon Evans
Nov 13 '18 at 22:48
It doesn't work. I didn't mention about that, but I'm generating DataTable dynamically and my DataGrid almost always have different columns and rows. That's why i can't set style for specific column.
– Wojtman
Nov 13 '18 at 22:57