为我的自定义RichTextBox提供通用控件

时间:2017-01-23 11:34:32

标签: c# wpf xaml

我创建了一个自定义RichTextBox,并希望提供一种简单易用的方法来在客户端添加EditingCommands,例如粗体,斜体,下划线: enter image description here

所以,我刚在我的MyRichTextBox中添加了此属性,并将其绑定到ItemsControl

中的Generic.xaml
public ItemCollection EditingCommands { get; } = new DataGrid().Items;

客户端中的用法看起来非常简单; - ):

<c:MyRichTextBox Text="{Binding Text, Mode=TwoWay}">
    <c:MyRichTextBox.EditingCommands>
        <Button Content="B" FontWeight="Bold" ToolTip="Bold: Ctrl+B" Width="20"
                Command="EditingCommands.ToggleBold" />
        <Button Content="I" FontStyle="Italic" ToolTip="Italic: Ctrl+I" Width="20"
                Command="EditingCommands.ToggleItalic" />
        <Button ToolTip="Underline: Ctrl+U" Width="20"
                Command="EditingCommands.ToggleUnderline">
            <TextBlock TextDecorations="Underline" Text="U" Padding="0" Margin="0" />
        </Button>
    </c:MyRichTextBox.EditingCommands>
</c:MyRichTextBox>

但是我想多次重用控件,所以我想用一种样式设置EditingCommands,例如

<Style x:Key="MyRichTextBoxStyle" TargetType="{x:Type c:MyRichTextBox}">
    <Setter Property="EditingCommands">
        <Setter.Value>
            <Button Content="B" FontWeight="Bold" ToolTip="Bold: Ctrl+B" Width="20"
                Command="EditingCommands.ToggleBold" />
            <Button Content="I" FontStyle="Italic" ToolTip="Italic: Ctrl+I" Width="20"
                Command="EditingCommands.ToggleItalic" />
            <Button ToolTip="Underline: Ctrl+U" Width="20"
                Command="EditingCommands.ToggleUnderline">
                <TextBlock TextDecorations="Underline" Text="U" Padding="0" Margin="0" />
            </Button>
        </Setter.Value>
    </Setter>
</Style>

但这是不可能的,因为EditingCommands不是依赖属性。转换为依赖项属性不适合类型ItemCollection

其实我想知道:这是否是主要目标的正确轨道?将Button放入ItemCollection?谁有更好的主意?也许ButtonModelDataTemplate一致?

1 个答案:

答案 0 :(得分:1)

  

这是主要目标的正确轨道吗?将按钮放入ItemCollection?有没有人有更好的主意?

您可以创建自己的自定义集合,并将此类型的依赖项属性添加到自定义RichTextBox类:

public class CustomItemCollection : ObservableCollection<UIElement>
{
}

public class MyRichTextBox : RichTextBox
{
    public static readonly DependencyProperty EditingCommandsProperty =
         DependencyProperty.Register("EditingCommands", typeof(CustomItemCollection),
         typeof(MyRichTextBox));

    public CustomItemCollection EditingCommands
    {
        get { return (CustomItemCollection)GetValue(EditingCommandsProperty); }
        set { SetValue(EditingCommandsProperty, value); }
    }
}

然后,您可以在样式设置器中设置属性:

<Style x:Key="MyRichTextBoxStyle" TargetType="{x:Type c:MyRichTextBox}" x:Shared="False">
    <Setter Property="EditingCommands">
        <Setter.Value>
            <c:CustomItemCollection>
                <Button Content="B" FontWeight="Bold" ToolTip="Bold: Ctrl+B" Width="20" Command="EditingCommands.ToggleBold" />
                <Button Content="I" FontStyle="Italic" ToolTip="Italic: Ctrl+I" Width="20" Command="EditingCommands.ToggleItalic" />
                <Button ToolTip="Underline: Ctrl+U" Width="20" Command="EditingCommands.ToggleUnderline">
                    <TextBlock TextDecorations="Underline" Text="U" Padding="0" Margin="0" />
                </Button>
            </c:CustomItemCollection>
        </Setter.Value>
    </Setter>
</Style>

显示按钮是修改MyRichTextBox类的模板以包含绑定到EditingCommands属性的ItemsControl的问题,例如:

<c:MyRichTextBox>
    <c:MyRichTextBox.Style>
        <Style TargetType="c:MyRichTextBox" BasedOn="{StaticResource MyRichTextBoxStyle}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="c:MyRichTextBox">
                        <StackPanel>
                            <ItemsControl ItemsSource="{TemplateBinding EditingCommands}" HorizontalAlignment="Right">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                            <RichTextBox />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </c:MyRichTextBox.Style>
</c:MyRichTextBox>