我可以为按钮的内容应用不同的样式吗?例如,我有一个搜索框,如果用户在搜索框中输入“是”并单击搜索,我可以在按钮内容下面的两个“是”斜体字体吗?与此同时,我希望保留其余部分,不要斜体。
<Button
Content="This is an example of button content. This is a very long content.">
<Button.Style>
???
</Button.Style>
</Button>
应用我希望按钮内容的样式后: 此是按钮内容的示例。这个 是一个非常长的内容。
这可能吗?
答案 0 :(得分:1)
Content
可以是任何内容,例如TextBlock
每个单词或文本片段包含Run
,然后您可以单独设置样式。我不认为纯XAML方法在这里非常有用。如果您想在某个地方的资源中定义突出显示样式,那么在用户输入时在代码中有条件地应用该样式。
答案 1 :(得分:1)
如果你的内容是静态的,你可以简单地定义你的按钮:
<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>
<RichTextBox IsReadOnly="True" BorderThickness="0"> <!-- Other styles as needed -->
<FlowDocument>
<Paragraph>
This
<Italic>is</Italic> an example of button content. This
<Italic>is</Italic> a very long content.
</Paragraph>
</FlowDocument>
</RichTextBox>
</Button>
但是,对于风格动态的情况,我使用以下服务:
// Note: Not my code, but I can't find the original source
public static class RichTextBoxService
{
public static string GetContent(DependencyObject obj)
{
return (string)obj.GetValue(ContentProperty);
}
public static void SetContent(DependencyObject obj, string value)
{
obj.SetValue(ContentProperty, value);
}
public static readonly DependencyProperty ContentProperty =
DependencyProperty.RegisterAttached("Content",
typeof(string),
typeof(RichTextBoxHelper),
new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
PropertyChangedCallback = OnDocumentChanged,
});
private static void OnDocumentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var richTextBox = (RichTextBox)obj;
// Parse the XAML content to a document (or use XamlReader.Parse())
var xaml = GetContent(richTextBox);
var doc = new FlowDocument();
var range = new TextRange(doc.ContentStart, doc.ContentEnd);
range.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), DataFormats.Xaml);
richTextBox.Document = doc;
// When the document changes update the source
range.Changed += (s, args) =>
{
if (richTextBox.Document == doc)
{
MemoryStream buffer = new MemoryStream();
range.Save(buffer, DataFormats.Xaml);
SetContent(richTextBox, Encoding.UTF8.GetString(buffer.ToArray()));
}
};
}
}
然后,您可以像按钮一样使用它:
查看:强>
<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;
}
}
}
答案 2 :(得分:0)
我只需定义您的按钮,如下所示:
ProprietaryName
然后我会使用ValueConverter来检查用户是否按下了另一个按钮来改变样式。
这样的事情:
mediawiki
您必须添加逻辑才能将其转换回正常