텍스트 블록에 바인딩이있는 하드 코딩 된 텍스트가 있습니다
-
16-09-2019 - |
문제
WPF에는 Text
a의 속성 TextBlock
하드 코드 텍스트와 특정 바인딩을 모두 포함하려면?
내가 생각하는 것은 다음의 선을 따라 무언가입니다.물론, 아래는 컴파일하지 않습니다):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
해결책
.NET 3.5 SP1에있는 경우
<TextBlock Text="{Binding Path=Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />
다른 팁
위의 접근 방식을 사용하면 다음과 같습니다.
<TextBlock Text="{Binding Path="Artist.Fans.Count",
StringFormat='Number of Fans: {0}'}" />
나는 StringFormat 내부의 대담한 얼굴을 찾을 수있는 방법을 찾지 못하거나 StringFormat에서 Apostrophe를 사용할 수 없다는 점에서 다소 제한적이라는 것을 알았습니다.
대신 나는이 접근법을 가지고 갔다.
<TextBlock TextWrapping="Wrap">
<Run>The value</Run>
<Run Text="{Binding Path=MyProperty1, Mode=OneWay}" FontWeight="Bold" />
<Run>was invalid. Please enter it with the format... </Run>
<LineBreak/><LineBreak/>
<Run>Here is another value in the program</Run>
<Run Text="{Binding Path=MyProperty2, Mode=OneWay}" FontWeight="Bold" />
</TextBlock>
<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>
여기서는 바인딩 값 (clouds.all)에 "%"가 추가됩니다. " {0 }"이후에 원하는 값을 추가 할 수 있습니다.
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>
템플릿 10 및 MVVM을 사용하는 XAML :
명확히하기 위해 :
- 정의상, 결합은 값을 대조군의 특성에 바인딩합니다.
- 'Template 10'프레임 워크에 구현 된 MVVM 패러다임에 따라 값은 관련 XAML 페이지와 관련된 뷰 모델에서 초기화됩니다.
다음은 텍스트 속성의 바인딩과 함께 하드 코딩 된 텍스트를 갖는 방법입니다.
<Page
...
xmlns:vm="using:doubleirish.ViewModels"
xmlns:sys="using:System"
xmlns:controls="using:Template10.Controls"
...
<Page.DataContext>
<vm:StocksViewModel x:Name="ViewModel" />
</Page.DataContext>
...
<controls:PageHeader ... Text="{x:Bind sys:String.Format('Ticker : {0}', ViewModel.Ticker)}">
...
</Page>
제휴하지 않습니다 StackOverflow