Datagrid Row background binding issue
-
Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:
Can anyone help me with this issue? Thanks, Best regards, Eli
-
Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:
Can anyone help me with this issue? Thanks, Best regards, Eli
-
Hi Background and AlternativeRowBackground isn't good enough. As I mentioned before , the color of each row shall be retreived from the model(stored in the database). Also,there can be more than 2 users,and each user have it's own color. Tanks for your help, Eli
-
Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:
Can anyone help me with this issue? Thanks, Best regards, Eli
You will need to create a
RowStyle
for your DataGrid and bind the Background property to the ColourProperty on your class. Something like this:-<DataGrid ItemsSource="{Binding Path=PostCollection}" AutoGenerateColumns="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Path=RowColour}"/>
</Style>
</DataGrid.RowStyle><DataGrid.Columns> <DataGridTextColumn Header="UserName" Binding="{Binding UserName}"/> <DataGridTextColumn Header="Status" Binding="{Binding Status}"/> </DataGrid.Columns> </DataGrid>
This obviously assumes the colour property of your user class is called RowColour and that it returns a
Brush
type. If not you will need to provide a converter if your property returns a Color or string type. Hope this helpsWhen I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
You will need to create a
RowStyle
for your DataGrid and bind the Background property to the ColourProperty on your class. Something like this:-<DataGrid ItemsSource="{Binding Path=PostCollection}" AutoGenerateColumns="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Path=RowColour}"/>
</Style>
</DataGrid.RowStyle><DataGrid.Columns> <DataGridTextColumn Header="UserName" Binding="{Binding UserName}"/> <DataGridTextColumn Header="Status" Binding="{Binding Status}"/> </DataGrid.Columns> </DataGrid>
This obviously assumes the colour property of your user class is called RowColour and that it returns a
Brush
type. If not you will need to provide a converter if your property returns a Color or string type. Hope this helpsWhen I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
It sure did...:thumbsup: Wow...I guess WPF is much more difficult than i thought. Thanks a lot.. Best regards, Eli
-
It sure did...:thumbsup: Wow...I guess WPF is much more difficult than i thought. Thanks a lot.. Best regards, Eli
Glad to help :)
eli15021979 wrote:
Wow...I guess WPF is much more difficult than i thought.
Once you get the hang of it, it becomes second nature, then you will be amazed at what you can accomplish with it.:thumbsup: Good luck.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman