2016-06-20 2 views
0

버튼의 처음 세 글자를 굵게 표시하고 밑줄을 그어 넣으시겠습니까?버튼의 첫 번째 글자의 스타일 형식

<Button Content="{Binding Path=ButtonText}"/> 

이 작업을 수행하는 방법을 알고 계십니까? 분명히 문자열 형식이 아닙니다 ...

답변

2

안녕하세요, 저는이 모든 것을 XAML로 할 수는 없지만 C#에서 약간의 도움으로 가능하다고 생각합니다. 여기 내가 어떻게 할 것입니까 :

Firs 속성을 Text로 DummyData라는 클래스를 만들고이 속성을 두 개의 별도 속성으로 나눕니다. 첫 번째는 내 텍스트의 첫 세 글자를 포함하고 다른 하나는 나머지 포함 : 그 때문에의 DataContext에

<Window 
    x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfApplication1" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    mc:Ignorable="d"> 
    <Grid> 
     <Button Width="200" Height="100"> 
      <TextBlock> 
       <Run FontWeight="Bold" Text="{Binding TestData.FirstThreeLetters, Mode=OneWay}" /><Run Text="{Binding TestData.RestOfTheText, Mode=OneWay}" /> 
      </TextBlock> 
     </Button> 
    </Grid> 
</Window> 

에주의 :

public DummyData TestData { get; set; } 

public MainWindow() 
{ 

    TestData = new DummyData() { Text = "This is a test data." }; 

    InitializeComponent(); 
} 

public class DummyData 
{ 
    public string Text { get; set; } 

    public string FirstThreeLetters 
    { 
     get 
     { 
      string result = string.Empty; 
      if (!String.IsNullOrEmpty(Text)) 
      { 
       result = Text.Substring(0, 3); 
      } 

      return result; 
     } 
    } 

    public string RestOfTheText 
    { 
     get 
     { 
      string result = string.Empty; 
      if (!String.IsNullOrEmpty(Text)) 
      { 
       result = Text.Substring(3); 
      } 

      return result; 
     } 
    } 
} 

마지막으로 우리는 다음과 같이 XAML에서 이러한 속성을 결합됩니다 내 코드 숨김에서 데이터를 바인딩하는 방법. 희망이 도움이됩니다.

+0

안녕하세요. 고마워, 훌륭한 해결책이야. 나는 디스플레이에 문제가 있지만 그것을 구현했습니다. 단어의 첫 번째 부분과 마지막 부분 사이에 약간의 공간이 있습니다. 왜 그럴 수 없는지 (그리고 마진이 없다는 것을 발견하지 못했습니다) – goul

+0

도움이 되셨다면 다행스럽게도 답변을 편집하고 코드를 공유 할 수있어서 다행 이군요. 이 코드가 작동하지 않으면 실행 사이에 공백을 두지 말고 한 줄에 넣으십시오. :). –

+0

나는 나의 대답을 편집 할 것이다. –

관련 문제