C# 如何在段落上应用不同的样式?

C# 如何在段落上应用不同的样式?,c#,wpf,user-interface,fonts,styles,C#,Wpf,User Interface,Fonts,Styles,我可以对按钮的内容应用不同的样式吗?例如,我有一个搜索框,如果用户在搜索框中输入“是”,然后单击搜索,我可以将下面的两个“是”用斜体字体显示在按钮内容中吗?同时,我想保持剩下的部分不变,没有斜体 <Button Content="This is an example of button content. This is a very long content."> <Button.Style> ??? </Button.Style>

我可以对按钮的内容应用不同的样式吗?例如,我有一个搜索框,如果用户在搜索框中输入“是”,然后单击搜索,我可以将下面的两个“是”用斜体字体显示在按钮内容中吗?同时,我想保持剩下的部分不变,没有斜体

<Button
    Content="This is an example of button content. This is a very long content.">
   <Button.Style>
    ???
   </Button.Style>
</Button>

???
应用样式后,我希望按钮内容: 这是按钮内容的一个示例。这是一个很长的内容


这可能吗?

内容可以是任何内容,例如,每个单词或文本段包含
运行
文本块
,然后可以单独设置样式。我不认为纯XAML方法在这里非常有用。如果您希望在参考资料中的某个地方定义突出显示样式,则在用户输入时在代码中有条件地应用该样式。

如果您的内容是静态的,您可以这样定义按钮:

<Button>
    <TextBlock>
         <Run Text="This"></Run>
         <Run Text="is" FontStyle="Italic"></Run>
         <Run Text="an example of button content. This"></Run>
         <Run Text="is" FontStyle="Italic"></Run>
         <Run Text="a very long content."></Run>
    </TextBlock>
</Button>
然后,您可以将其与按钮一起使用,如下所示:

<Button>
    <TextBlock>
         <Run Text="This"></Run>
         <Run Text="is" FontStyle="Italic"></Run>
         <Run Text="an example of button content. This"></Run>
         <Run Text="is" FontStyle="Italic"></Run>
         <Run Text="a very long content."></Run>
    </TextBlock>
</Button>
查看:

<Window xmlns:services="clr-namespace:MyProject.Services;assembly=MyProject">

    <Button>
        <RichTextBox IsReadOnly="True" BorderThickness="0" services:RichTextBoxService.Content="{Binding ButtonContent}" />
    </Button>
// Note: I can't remember is Section is required or not
private const string Header = @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><Paragraph>";

private const string DefaultContent = "This is an example of button content. This is a very long content";

private const string Footer = "</Paragraph></Section>";

private string _search;
public string Search
{ 
    get { return _search; }
    set {
         if (Set(ref _search, value))  // using MVVMLight
         {
             // search value was updated
             this.ButtonContent = Header + DefaultContent.Replace(value, "<Italic>" + value + "</Italic>") + Footer;
         }
    }
}

视图模型:

<Window xmlns:services="clr-namespace:MyProject.Services;assembly=MyProject">

    <Button>
        <RichTextBox IsReadOnly="True" BorderThickness="0" services:RichTextBoxService.Content="{Binding ButtonContent}" />
    </Button>
// Note: I can't remember is Section is required or not
private const string Header = @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><Paragraph>";

private const string DefaultContent = "This is an example of button content. This is a very long content";

private const string Footer = "</Paragraph></Section>";

private string _search;
public string Search
{ 
    get { return _search; }
    set {
         if (Set(ref _search, value))  // using MVVMLight
         {
             // search value was updated
             this.ButtonContent = Header + DefaultContent.Replace(value, "<Italic>" + value + "</Italic>") + Footer;
         }
    }
}
//注意:我不记得是否需要这个部分
私有常量字符串头=@“”;
private const string DefaultContent=“这是按钮内容的一个示例,这是一个非常长的内容”;
private const string Footer=“”;
私有字符串搜索;
公共字符串搜索
{ 
获取{return\u search;}
设置{
if(设置(ref _search,value))//使用MVVMLight
{
//搜索值已更新
this.ButtonContent=Header+DefaultContent.Replace(value,“+value+”)+Footer;
}
}
}

我将简单地定义您的按钮,如下所示:

                <Button.Content>
                <TextBlock>
                    <Run Text="This" />
                    <Run Text="is" FontStyle="{Binding Converter={StaticResource ItalicConverter}}" />
                    <Run Text="a..."  />
                </TextBlock>
            </Button.Content>

您必须添加逻辑才能将其转换回正常状态

如何在用户输入时有条件地在代码中应用样式?你能详细谈谈这一点吗?或者你有一个例子让我参考一下吗?这只是意味着如果某些条件成立,则分配
Style
属性,例如
if(run.Text.Contains(“the”))run.Style=italicStyle
。酷!!我要试试这个!非常感谢你!如果有任何错误,请告诉我。我对我使用的代码做了一些修改,但还没有测试它的编译能力。你有没有在斜体部分尝试过更奇特的样式?您知道我可以对该值应用任何样式吗?或者只是简单的样式,比如字体等等。?DefaultContent.Replace(value,“+value+”)是的,您可以使用
@“+value+”
,而不是使用
,例如,这太棒了!非常感谢。谢谢你的回答。实际上,内容是动态的,搜索内容也是动态的。但还是要谢谢你!