Diskuze: Binding RichTextBox with ColorPicker for ForeColor and BackColor (selected text)

C# .NET .NET (C# a Visual Basic) Binding RichTextBox with ColorPicker for ForeColor and BackColor (selected text) American English version English version

Avatar
Franc Beran
Člen
Avatar
Franc Beran:

Zdravím všechny a prosím o radu. Používám WPF Extended Toolkit, dva ColorPickers vlevo mění ForeColor a BackColor celého textu v RichTextBoxu, ComboBox pro změnu fontů a velikosti fontu to opět mění pro celý RichTextBox, Bold, Italic a Underline je aplikován na selected text a ty další dva ColorPickers by měli být pro změnu ForColor a BackColor na selected text. Tápu jak mám vytvořit binding u těch dvou posledních ColorPickers. Mohli byste mi pomoci, stačí mi ten kompletní binding. Přikládám zdroják. Díky moc.

XAML:
<Window xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  x:Class="WPFContextMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:wpfx="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFContextMenu"
        mc:Ignorable="d"
        Title="ContextMenuWithCommandsSample" Height="500" Width="1080" Loaded="Window_Loaded">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.SelectAll" />
    </Window.CommandBindings>
<DockPanel>
        <ToolBar DockPanel.Dock="Top" Height="30">
            <wpfx:ColorPicker Name="CPForeColor" DisplayColorAndName="True" Height="25" Width="180"
                              SelectedColorChanged="CPForeColor_SelectedColorChanged"
                              FontWeight="Bold" FontSize="14" />
            <wpfx:ColorPicker Name="CPBackColor" DisplayColorAndName="True" Height="25" Width="180"
                              SelectedColorChanged="CPBackColor_SelectedColorChanged"
                              FontWeight="Bold" FontSize="14" />
            <Separator />
            <ComboBox Name="cmbFontFamily" Height="25" Width="150" SelectedIndex="304"
                      SelectionChanged="cmbFontFamily_SelectionChanged" FontWeight="Bold" FontSize="14" />
            <ComboBox Name="cmbFontSize" Height="25" Width="50" SelectedIndex="0"
                      IsEditable="True"
                      TextBoxBase.TextChanged="cmbFontSize_TextChanged" FontWeight="Bold" FontSize="14" />
            <ToggleButton Command="EditingCommands.ToggleBold"
                          Margin="5" Height="25" Name="btnBold">
                <Image Source="W:\EXTDB\NRImages\bold-icon.png" Width="16" Height="16" />
            </ToggleButton>
            <ToggleButton Command="EditingCommands.ToggleItalic"
                          Margin="5" Height="25" Name="btnItalic" FontWeight="Bold" FontStyle="Italic">
                <Image Source="W:\EXTDB\NRImages\italic-icon.png" Width="16" Height="16" />
            </ToggleButton>
            <ToggleButton Command="EditingCommands.ToggleUnderline"
                          Margin="5" Height="25" Name="btnUnderline">
                <Image Source="W:\EXTDB\NRImages\underline-icon.png" Width="20" Height="20" />
            </ToggleButton>
            <Separator />
            <wpfx:ColorPicker Name="CPForeColorSel" DisplayColorAndName="True" Height="25" Width="180"
                              SelectedColorChanged="CPForeColorSel_SelectedColorChanged"
                              FontWeight="Bold" FontSize="14" />
            <wpfx:ColorPicker Name="CPBackColorSel" DisplayColorAndName="True" Height="25" Width="180"
                              SelectedColorChanged="CPBackColorSel_SelectedColorChanged"
                              FontWeight="Bold" FontSize="14" />
        </ToolBar>
        <xctk:RichTextBox Name="rtbEditor" SelectionChanged="rtbEditor_SelectionChanged">
            <RichTextBox.ContextMenu>
            <ContextMenu>
                <MenuItem Command="Undo">
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\undo.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Command="Redo">
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\redo.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Command="Cut">
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\cut.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Command="Copy">
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\Copy.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Command="Paste">
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\Paste.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Command="ApplicationCommands.SelectAll" >
                    <MenuItem.Icon>
                        <Image Source="W:\EXTDB\NRImages\selectAll.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
            </RichTextBox.ContextMenu>
            </xctk:RichTextBox>
    </DockPanel>
</Window>
------------------------------------------------------------------------------------------------------------------------------
Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//using Xceed.Wpf.Toolkit;

namespace WPFContextMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            cmbFontFamily.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
            cmbFontSize.ItemsSource = new List<double>() { 20, 22, 24, 26, 28, 36, 48, 72 };
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            rtbEditor.FontSize = 20;
            rtbEditor.AppendText("Bla bla bla bla");
        }

        private void rtbEditor_SelectionChanged(object sender, RoutedEventArgs e)
        {
            object temp = rtbEditor.Selection.GetPropertyValue(Inline.FontWeightProperty);
            object _color = rtbEditor.Selection.GetPropertyValue(Inline.ForegroundProperty);

            btnBold.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontWeights.Bold));
            temp = rtbEditor.Selection.GetPropertyValue(Inline.FontStyleProperty);

            btnItalic.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontStyles.Italic));
            temp = rtbEditor.Selection.GetPropertyValue(Inline.TextDecorationsProperty);

            btnUnderline.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(TextDecorations.Underline));
            temp = rtbEditor.Selection.GetPropertyValue(Inline.FontFamilyProperty);

            cmbFontFamily.SelectedItem = temp;
            temp = rtbEditor.Selection.GetPropertyValue(Inline.FontSizeProperty);
            cmbFontSize.Text = temp.ToString();
        }

        private void cmbFontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbFontFamily.SelectedItem != null)
            {
                TextRange textRange = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
                textRange.ApplyPropertyValue(Inline.FontFamilyProperty, cmbFontFamily.SelectedItem);
                //rtbEditor.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, cmbFontFamily.SelectedItem);
            }

        }

        private void cmbFontSize_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextRange textRange = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
            textRange.ApplyPropertyValue(Inline.FontSizeProperty, cmbFontSize.Text);
            //rtbEditor.Selection.ApplyPropertyValue(Inline.FontSizeProperty, cmbFontSize.Text);
        }

        private void CPForeColor_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            rtbEditor.Foreground = new SolidColorBrush(CPForeColor.SelectedColor);
        }

        private void CPBackColor_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            rtbEditor.Background = new SolidColorBrush(CPBackColor.SelectedColor);
        }

        private void CPForeColorSel_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {

            // tady tápu chci to aplikovat jen na selected text
            //TextRange textRange = new TextRange(rtbEditor.Selection.Start, rtbEditor.Selection.End);
            //rtbEditor.Selection.ApplyPropertyValue(Inline.ForegroundProperty, CPForeColorSel.SelectedColor);
            //rtbEditor.SelectionBrush = new SolidColorBrush(CPBackColorSel.SelectedColor);
            rtbEditor.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush

(CPBackColorSel.SelectedColor));
        }


        private void CPBackColorSel_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            // tady tápu chci to aplikovat jen na selected text
        }
    }
}
 
Odpovědět 11.11.2015 15:03
Avatar
Franc Beran
Člen
Avatar
Odpovídá na Franc Beran
Franc Beran:

Zdravím všechny a dávám k dispozici svojí odpověď na problém, neboť slepé kuře našlo zrno na webu. Tohleto je řešení toho, co jsem chtěl:

private void CPForeColorSel_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            // pro červenou
            //rtbEditor.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            // pro výběr
            rtbEditor.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(CPForeColorSel.SelectedColor));
            //by pushing the button we lose focus, so give it focus again.
            rtbEditor.Focus();
        }


        private void CPBackColorSel_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            rtbEditor.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(CPBackColorSel.SelectedColor));
            //by pushing the button we lose focus, so give it focus again.
            rtbEditor.Focus();
        }
 
Nahoru Odpovědět 12.11.2015 7:10
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.