Home Forums WPF controls Xceed DataGrid for WPF Change style of rows depending on value of colum?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22621 |

    Hello,

    Thanks for the Xceed DataGrid for WPF – you guys rock!

    I’m working on WPF client that connects to a web service and retrieves a dataset. I then bind the dataset to the grid’s ItemsSource, that’s working great.

    Is there a way I can change the style of the rows depending on a value of a column? For example: in Outlook messages should appear in bold if they haven’t been read, otherwise they shouldn’t.

    Could I do something similar with Xceed’s datagrid?

    Here’s my current code:

    MyGrid.ItemsSource = MyResponse.ResultDataSet.Tables(0).DefaultView
    ‘ The first 4 columns should not be visible to the user
    Dim iColumn As Integer = 0
    For Each oColumn As Column In grdNotifications.Columns
    If iColumn <= 3 Then oColumn.Visible = False
    iColumn += 1
    Next

    If the value of column 2 (one of the “invisible” columns) is 1 then the whole row should be bold, otherwise it shouldn’t.

    I have a couple of ideas on how to do this, but I thought it would be a good idea to ask the experts on the forum first.

    Thanks again for this great WPF datagrid!

    Imported from legacy forums. Posted by Emmanuel (had 4594 views)

    User (Old forums)
    Member
    Post count: 23064

    No one from Xceed or the community has any suggestions?

    Imported from legacy forums. Posted by Emmanuel (had 230 views)

    Xceed Support
    Member
    Post count: 5658

    Hi,

    One easy what to achieve this result is by placing a Trigger in the DataRow style.

    The trigger can look upon the value you need and then react appropriatly:

    <Style>
    <Style.Triggers>
    <DataTrigger Binding=”{Binding Path=Read}” Value=”1″>
    <Setter Property=”TextElement.FontWeight” Value=”Bold”/>
    </DataTrigger >
    </Style.Triggers>
    </Style>

    This is possible because the DataRowView (data item for the Row) has property descriptors for each column.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 408 views)

    User (Old forums)
    Member
    Post count: 23064

    Marcus,

    Thanks for the response. I tried it out but got an exception – I probably didn’t place the above XAML in the correct place. I need to read up on the documentation to understand triggers, setter properties, etc…

    In the meantime, could I do this in code at runtime? In other words, how would I achieve the same in C# or VB.Net? This is important because the “bold” feature was just an example – in fact, my app has business requirements that are a bit more complicated and dynamic (they feature changes depending on data).

    Imported from legacy forums. Posted by Emmanuel (had 201 views)

    Xceed Support
    Member
    Post count: 5658

    Because of UI virtualization (which prevents creating UI Elements for all of your data items [ for performance and memory consumption reasons] ) it would be rather complicated to dynamically set, in code, complex aspects.

    Still, the use of triggers, multi-triggers, data-triggers can achieve pretty complex result themselves…

    As to how the “quick style sample” provided is supposed to be plugged in your application: First of all, it was really quickly coded just to show you the global idea, it was not even compiled… But I guess I can give you a little more information:

    You can either apply the style to the “DataGridControl.ItemContainerStyle” property (which will be applied to all DataRows). Or you can place the style in the ResourceDictionary of the DataGridControl and make it “implicit” ( <i><Style TargetType=”{x:Type xcdg:DataRow}”> </i> )

    Imported from legacy forums. Posted by Marcus [Xceed] (had 247 views)

    User (Old forums)
    Member
    Post count: 23064

    Marc,

    Thanks for all of your explanations and examples. After I dug in and understood ResourceDictionaries and data-triggers, it worked great!

    For those that might want to do the same, I used the sample code in:

    \Xceed Component Samples\Xceed DataGrid for WPF 1.0\Samples\Visual Basic .NET\CustomViews

    I created my own CustomTableView.xaml (based on the example but with the logic for making a row bold or normal) and added it as a ResourceDictionary. In my constructor I then added:

    Me.Resources.Remove(GetType(DataRow))
    Me.Resources.Add(GetType(DataRow), Me.FindResource(“customTableViewDataRowStyle”))

    and voila!! As I change the underlying dataview bound to the datagridcontrol the UI reflects what I defined in the ResourceDictionary!

    This is truly following the WPF philosophy and the first time you see it working it’s like magic!

    THANKS!

    Imported from legacy forums. Posted by Emmanuel (had 773 views)

    Xceed Support
    Member
    Post count: 5658

    Quoting ehuna:
    <i>This is truly following the WPF philosophy and the first time you see it working it’s like magic! </i>

    Thank you for you kind comment. We indeed placed great deal of efforts trying to make the DataGridControl blend within the WPF concepts. And I’m glad that you’re also beginning to see the “WPF light”!!!

    Going-up the WPF learning cliff is rather hard, but once your uphill, you really can enjoy the fun!

    Imported from legacy forums. Posted by Marcus [Xceed] (had 5326 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.