none
attaching/binging style of UI element in DataGridTemplateColumn to grid style RRS feed

  • Question

  • Hi,

    i have a wpf datagrid with a template columns that have a button , i change the style of the cell dynamically throw code behind depending of user info ,

    my problem is: how can i change the style of the button inside the template columns ?

    can i access it from code behind ? or how can i add/attach his style to grid cell style , so when the cell style changes the button style changes as will

    regards

     <DataGrid x:Name="booksDataGrid"
                      AutoGenerateColumns="False"
                      EnableRowVirtualization="True"
                      ItemsSource="{Binding Source={StaticResource bookViewSource}}"
                      RowDetailsVisibilityMode="VisibleWhenSelected"
                      Style="{StaticResource DataGridBaseStyleFull}"
                      CellStyle="{DynamicResource PresenterBooksGridCellSelection}"
                      ColumnHeaderStyle="{DynamicResource PresenterBooksGridColumnHeader }">
    
     <DataGrid.Columns>
    <DataGridTemplateColumn Header=""
                                            x:Name="bookButtonsLocationColmun">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
    
                                <Button Content="Location"
                                       Name="BookGridLocationButton"                                    
                                        Style="{DynamicResource location_red}"
                                        
                                        Margin="10,0" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
     </DataGrid.Columns>
     </DataGrid>


    Thursday, December 3, 2015 5:39 AM

Answers

  • Hi Hani,

    That's your first name, right?

    .

    You are replacing the cell template with your datatemplate:

    <DataGridTemplateColumn.CellTemplate>


    But there is still a cell that button goes in and it will still use properties which you're not setting in the template and which are appropriate.

    This is probably clearer if I just show you some simple markup.

    I have an experimental datagrid which has all sorts of stuff in it. I've used this for other people's questions.

    If I set a style on the cell:

        <Window.Resources>
            <Style TargetType="DataGridCell" x:Key="dgc">
                <Setter Property="Foreground" Value="Red"/>
            </Style>
        </Window.Resources>
        <StackPanel>
                <DataGrid 
                    CellStyle="{StaticResource dgc}"

    Then all the foregrounds change to red on all the stuff in the datagrid.

    Including a button I have:

                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Command="{Binding ChangeNameCommand}" Content="Change Name"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

    And all the cells in the body of the datagrid get that foreground.

    Like I said - lot of stuff in there.

    Take that back out and everything goes back.

    Note that this takes precedence over  elementstyle, which might be something you could find useful.

    Here are the triggers which set the colour on that first column

                    <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextAlignment" Value="Center"/>
                            <Style.Triggers>
                                <Trigger Property="Text" Value="OFFLINE">
                                    <Setter Property="Background" Value="Red"/>
                                </Trigger>
                                <Trigger Property="Text" Value="ONLINE">
                                    <Setter Property="Background" Value="Green"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Hani Safa Thursday, December 3, 2015 10:05 AM
    Thursday, December 3, 2015 8:06 AM
    Moderator

All replies

  • Hi Hani,

    That's your first name, right?

    .

    You are replacing the cell template with your datatemplate:

    <DataGridTemplateColumn.CellTemplate>


    But there is still a cell that button goes in and it will still use properties which you're not setting in the template and which are appropriate.

    This is probably clearer if I just show you some simple markup.

    I have an experimental datagrid which has all sorts of stuff in it. I've used this for other people's questions.

    If I set a style on the cell:

        <Window.Resources>
            <Style TargetType="DataGridCell" x:Key="dgc">
                <Setter Property="Foreground" Value="Red"/>
            </Style>
        </Window.Resources>
        <StackPanel>
                <DataGrid 
                    CellStyle="{StaticResource dgc}"

    Then all the foregrounds change to red on all the stuff in the datagrid.

    Including a button I have:

                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Command="{Binding ChangeNameCommand}" Content="Change Name"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

    And all the cells in the body of the datagrid get that foreground.

    Like I said - lot of stuff in there.

    Take that back out and everything goes back.

    Note that this takes precedence over  elementstyle, which might be something you could find useful.

    Here are the triggers which set the colour on that first column

                    <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextAlignment" Value="Center"/>
                            <Style.Triggers>
                                <Trigger Property="Text" Value="OFFLINE">
                                    <Setter Property="Background" Value="Red"/>
                                </Trigger>
                                <Trigger Property="Text" Value="ONLINE">
                                    <Setter Property="Background" Value="Green"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Hani Safa Thursday, December 3, 2015 10:05 AM
    Thursday, December 3, 2015 8:06 AM
    Moderator
  • thanks a lot for your time,

    yep my first name is Hani.

    the description you provide will make me control the style of a specific cell , but what if the cell has 2 buttons, each has its own style

    further more , there are button style property that do not exist on the cell , like creating mouse over and clicks .

    right now , i have 3 template cells each has a button that have a different style , when i change the style of the grid i show the cell that correspond to that style .

    Thursday, December 3, 2015 9:40 AM
  • You're putting 2 buttons in one cell?

    That doesn't sound like a good plan.

    You can associate all sorts of stuff with buttons by style and you could use based on to define a base button style.

    You could also use template to define a different template for some button or several.

    If things don't apply to the cell then put them on the button style.

    If you want to use the style from the  cell in the button either use common things like the same brush resource or bind.

    If you want to use common brushes or something then define the brushes and use them in your multiple styles.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, December 3, 2015 9:59 AM
    Moderator