あなたのコンテンツが静的だった場合、あなたは、単にそのようなあなたのボタンを定義することができます。
<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>
のViewModel:にValueConverterを
<Button.Content>
<TextBlock>
<Run Text="This" />
<Run Text="is" FontStyle="{Binding Converter={StaticResource ItalicConverter}}" />
<Run Text="a..." />
</TextBlock>
</Button.Content>
をそして、私が使用します。私は単純に以下のようなあなたのボタンを定義します
// 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;
}
}
}
クール!!私はこれを試してみよう!どうもありがとうございます! – spspli
エラーがある場合は教えてください。私が使っているコードからいくつかの変更を加え、コンパイルしたことをテストしていません。 – Pakman
イタリック体の部分でさらに派手なスタイルを試したことがありますか?私がこの価値にしたいスタイルを適用することができますか?フォントなどの単純なスタイルですか? DefaultContent。"" + value + " " –
spspli