2016-07-27 4 views
1

ボタンのコンテンツにさまざまなスタイルを適用できますか?例えば、私は検索ボックスを持っています。ユーザーが検索ボックスに「is」と入力して検索をクリックすると、ボタンコンテンツイタリックフォントの下にある「is」を2つ作ることができますか?同時に、残りの部分はそのまま、斜体は残したいと思います。 このはボタンの内容の一例です:段落に異なるスタイルを適用するにはどうすればよいですか?

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

した後、私はボタンの内容をするスタイルを適用します。このです。

これは可能ですか?

答えて

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> 

の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; 
     } 
    } 
} 
+0

クール!!私はこれを試してみよう!どうもありがとうございます! – spspli

+0

エラーがある場合は教えてください。私が使っているコードからいくつかの変更を加え、コンパイルしたことをテストしていません。 – Pakman

+0

イタリック体の部分でさらに派手なスタイルを試したことがありますか?私がこの価値にしたいスタイルを適用することができますか?フォントなどの単純なスタイルですか? DefaultContent。"" + value + "" – spspli

1

Contentは、何でもかまいません。単語またはテキストセグメントごとにRunを含むTextBlockを個別にスタイル設定することができます。純粋なXAMLアプローチはここでは非常に便利だとは思いません。リソース内の強調表示スタイルをどこかで定義したい場合は、ユーザー入力時に条件付きでコードにそのスタイルを適用します。

+0

正常に戻ってそれを変換するロジックを追加する必要があります

public class ItalicConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return "Italic"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return true; } } 

:このような

何か?あなたはこれについてもっと話すことができますか、私が参照できるように例がありますか? – spspli

+0

何らかの条件が成立している場合には 'Style'プロパティを代入するだけです。 'if(run.Text.Contains(" the "))run.Style = italicStyle'です。 –

0

ユーザーがもう一方を押したかどうかを確認するボタンを押してスタイルを変更します。あなたはユーザーの入力時にコードに条件付きスタイルを適用する方法

+0

あなたの答えをありがとう。実際、コンテンツは動的であり、検索コンテンツも動的です。しかし、まだありがとう! – spspli

関連する問題