2011-02-09 94 views
19

我在想什麼,並找不到任何相關的主題。我有以下綁定:是否可以在xaml中綁定後添加更多字符?

Content="{x:Static resx:Resource.Form_OtherOption_Description}" 

這將在標籤中放置一個字符串。我問自己的是,如果我可以在綁定後添加「:」,而不是在代碼中,只需在xaml中添加。標籤代表「Name:」之類的內容。但添加「:」作爲綁定的一部分不是一種選擇。

編輯

我在3.5版本中

任何建議工作。

在此先感謝。

+0

如果你在3.5,你很可能需要使用@ H.B.的解決方案。 – user7116 2011-02-10 15:05:39

回答

30

你可能喜歡的東西做到這一點。我找到的已移至<Label>上的ContentStringFormat屬性。

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}" 
     ContentStringFormat="{}{0}:" /> 
+1

+1,看到你找到答案了:)所以`ContentControl`有`ContentStringFormat`,`HeaderedContentControl`有`HeaderStringFormat`,'ItemsControl`有`ItemStringFormat`。相關提示 – 2011-02-09 16:47:05

+1

出現`Label.Content`不會從綁定中獲取`StringFormat`,因爲`TargetType`不是`string`,而是`object`。在「TextBlock.Text」的情況下,目標類型是「string」,因此它在綁定中使用。 – user7116 2011-02-09 17:44:51

0

您可以創建一個接收輸入字符串並添加「:」的轉換器。

public class AddStringToStringConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string input = value as string; 
     string suffix = parameter as string; 

     return input + suffix; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

的XAML:

<Window.Resources> 
    <local:AddStringToStringConverter x:Key="AddStringToStringConverter"/> 
</Window.Resources> 
... 
<Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/> 

或者類似的東西。試過了,至少它對我的來源有效。

如果你有空白等在你身邊ConverterParameter你可以使用引號引號來確保它不會被丟棄。

編輯:哦,對了...是啊...還有還有StringFormat這是我以前從來沒有需要,ehehehe ...

3

嘗試綁定的屬性StringFormat - 它可以做的非常簡單,你想要什麼。

<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, 
         StringFormat={}{0}:}" /> 

編輯:<Label>小號Content財產不尊重綁定的StringFormat財產顯然

9

您還可以使用MultiBinding用的StringFormat例如:

<TextBlock> 
    <TextBlock.Text> 
     <MultiBinding StringFormat="ID {0} Name: {1} Age: {2}"> 
      <Binding Source="{x:Static resx:SomeResx.ID}"/> 
      <Binding Path="Name"/> 
      <Binding Path="Age"/> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

您可以在內容控制的TextBlock TextBlock.Text(用這個抱歉,我不能得到的代碼,以示對本以上)

19

如果你使用WPF 4.0,你也可以這樣做:

<TextBlock> 
     <Run Text="{Binding SomeLabel}"/> 
     <Run Text=":"/> 
</TextBlock> 

這其實連接來自兩個Run標記的兩個字符串並複製到TextBlock.Text屬性!

使用此方法,您甚至可以綁定到演示者中的不同屬性,並將其顯示在單個TexBlock中。看到這個很好的例子:

Can we concat two properties in data binding?

3

當然可以。這裏我在windows phone中綁定文本(clouds.all)後添加「測試」。

<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/> 
1

,如果你使用標籤的進度條裏面,你可以用這樣的方式:

<Label x:Name="Progress" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Foreground="White" Opacity=".7" 
     Content="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}%"> 

這樣你可以添加一個形象化%進度的值。

相關問題