简体   繁体   English

在WPF中设置控件的命令

[英]Set commands for a control in WPF

I am using DevExpress for a WPF application, I want to hit a toggle button and make the contents in a richTextBox bold, the manual which is provided here suggests to use ToggleFontBoldCommand as the command. 我正在将DevExpress用于WPF应用程序,我想单击一个切换按钮并使richTextBox的内容richTextBox粗体, 此处提供的手册建议使用ToggleFontBoldCommand作为命令。 In my WPF application in the xaml file I have added the following code: xaml文件的WPF应用程序中,我添加了以下代码:

<telerik:RadToggleButton CommandTarget="{Binding ElementName=doc}" Command="com:ToggleFontBoldCommand" Content="B"/>

doc is a richTextBox. doc是richTextBox。 com is the namespace of DevExpress commands' namespaces (xmlns:com="clr-namespace:DevExpress.XtraRichEdit.Commands; assembly=DevExpress.RichEdit.v16.1.Core") . com是DevExpress命令的命名空间的命名空间(xmlns:com="clr-namespace:DevExpress.XtraRichEdit.Commands; assembly=DevExpress.RichEdit.v16.1.Core")
The point is that Visual Studio can not find the command. 关键是Visual Studio无法找到该命令。 I believe that I am wrong with the CommandTarget but I do not know what is wrong. 我相信CommandTarget是错误的,但我不知CommandTarget什么问题。

I'm just using the standard WPF ToggleButton and TextBox in this answer, but I believe the solution should also work with your telerik and DevExpress controls. 我在此答案中仅使用标准的WPF ToggleButtonTextBox ,但我相信该解决方案也应与您的telerikDevExpress控件一起使用。

If all you want is to make the text in the RichTextBox bold when the RadToggleButton has been clicked you should be able to side step the whole command thing and use something similar to the following: 如果只需要在单击RadToggleButton时使RichTextBox的文本RadToggleButton 粗体 ,则您应该可以使整个命令RadToggleButton并使用类似于以下内容的命令:

<ToggleButton x:Name="BoldToggle" />
<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="FontWeight" Value="Normal"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=BoldToggle}" Value="true">
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

The style on the TextBox changes the FontWeight property from "Normal" to "Bold" when the ToggleButton 's IsChecked property is "true". ToggleButtonIsChecked属性为“ true”时, TextBox上的样式会将FontWeight属性从“ Normal”更改为“ Bold”。

Does this get you what you need? 这能给您您所需要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM