locked
How to Bind to ConverterParameter RRS feed

  • Question

  • Hi

    I would like to bind to a datagrid header ans as the Header property is not a dependency property there is no way to bind directly to this property. I try to workaround using a HeaderStyle and i end up to the case where i need to bind to ConverterParameter. I read a lot about that and it seems that it is impossible to bind to this property.

    Do you have any solution to workaround?

     

    In my situation each header has a number (in this exemple 65) and i use this number in the conveter to lookup the word.

    Here is my code

          <sdk:DataGrid x:Name="listModelCalcul" Margin="5" IsReadOnly="True" Foreground="Black" AlternatingRowBackground="AliceBlue" SelectedItem="{Binding CurrentModelCalcul,Mode=TwoWay}" AutoGenerateColumns="False" Grid.Row="1" ItemsSource="{Binding ElementName=modelCalculDomainDataSource, Path=Data}" >
                                <sdk:DataGrid.Columns>
                                    <sdk:DataGridTextColumn    Header="65" HeaderStyle="{StaticResource nomColumnHeader}" Width="60*" Binding="{Binding nom}"/>
    
                                                               </sdk:DataGrid.Columns>
               </sdk:DataGrid>
     
     
        <Style x:Key="nomColumnHeadr" TargetType="prim:DataGridColumnHeader">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="FontWeight" Value="bold"/>
            <Setter Property="Height" Value="40"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <ContentPresenter>
                                <TextBlock Text="{Binding Path=Words, Source={StaticResource ResourceWrapper},Converter={StaticResource WordConverter},ConverterParameter=65,Mode=TwoWay}"/>
    
                            </ContentPresenter>
      
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
           </Style>

    Any help will be appreciated

    Thanks

     

    Thursday, July 22, 2010 6:38 AM

Answers

  • I also was struggling with the data grid header in the past and spent hours figuring out the most elegant ways to set it (e.g. in localized applications). Probably the easiest way really is to do some work in code-behind. To avoid hard-coded manipulation of headers, you could do something like this:

    1. In the Xaml, store the key of the resource you want to bind.
    2. In code-behind, loop over the columns of your grid and fetch and set the correct string, using the key and the resource manager of your resources.

    If you want to avoid writing the same code over and over again, you could make a behavior from that and attach it in Xaml.

    Thursday, July 22, 2010 1:14 PM

All replies

  • I also was struggling with the data grid header in the past and spent hours figuring out the most elegant ways to set it (e.g. in localized applications). Probably the easiest way really is to do some work in code-behind. To avoid hard-coded manipulation of headers, you could do something like this:

    1. In the Xaml, store the key of the resource you want to bind.
    2. In code-behind, loop over the columns of your grid and fetch and set the correct string, using the key and the resource manager of your resources.

    If you want to avoid writing the same code over and over again, you could make a behavior from that and attach it in Xaml.

    Thursday, July 22, 2010 1:14 PM
  • I came to this when I was searching for a solution to Bind to ConverterParameter. So for those in the same boat:

    Use a multibinding instead (from ScottLogic's SLMultiBinding Package). More explained in my blog.

    Monday, August 9, 2010 12:47 AM