Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi !

I'm using a DataTemplateSelector to allow users to edit several items in my software. All these items can be of several types.

My DataTemplates mainly consist of a viewer (TextBox, Image,...) and some buttons dealing actions on the DataContext. I access to it using code like this :

C#
var DataItem = ((FrameworkElement)sender).DataContext as MyItemType;


It works very well for the vast majority of my items, but I've problem with the RichTextBox case : some of my buttons have to access data, not in the DataContext, but in the RichTextBox.Selection

My code looks like that :


HTML
<DataTemplate x:Key ="TexteTemplate">
        <Border>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button  Content="G" TextElement.FontWeight="Bold" Click="RichTextGras_Click" />
                    <Button Content="I" TextElement.FontStyle="Italic" Click="RichTextItalic_Click" />
                    <Button Content="S" Click="RichTextSouligné_Click" />
                </StackPanel>
                <RichTextBox Grid.Row="1" Document="{Binding Path=Text}" x:Name ="RTBox"/>
            </Grid>
        </Border>
    </DataTemplate>



(only the essential was kept)

Thanks for your help !
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900