none
在XAML文件里,可不可以直接对字符串进行一些操作 RRS feed

  • 问题

  • 假设我有个label控件,我想在在里面直接拼接一些字符串,请问有什么方法可以做到?

    谢谢

    2011年3月10日 0:48

答案

  • 哈,中午试出了一招

    <Window x:Class="T_Mar10.MainWindow"
    		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:sys="clr-namespace:System;assembly=mscorlib"
    		Title="MainWindow" Height="350" Width="525">
    	<Window.Resources>
    		<ObjectDataProvider x:Key="strcat"
    							ObjectType="{x:Type sys:String}"
    							MethodName="Concat">
    			<ObjectDataProvider.MethodParameters>
    				<sys:String>aaa</sys:String>
    				<sys:String>bbb</sys:String>
    				<sys:String>ccc</sys:String>
    			</ObjectDataProvider.MethodParameters>
    		</ObjectDataProvider>
    	</Window.Resources>
    	<Grid>
    		<Label>
    			<Label.Content>
    				<Binding Source="{StaticResource strcat}"/>
    			</Label.Content>
    		</Label>
    	</Grid>
    </Window>
    2011年3月10日 6:12
  • 如果你就是要简单的拼接,那你完全可以用TextBlock,而不是Label,这样你可以用Run去拼接字符串在TextBlock中,而且不同的Run可以应用不同的字体格式,更加方便。比如:

    <TextBlock>

        <Run>A</Run>

        <Run>B</Run>

    </TextBlock>

     

     

     


    Sheldon _Xiao[MSFT]

    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手

    2011年3月10日 10:23
    版主

全部回复

  • 方法有2:

    1)你参考绑定的StringFormat这个属性如何使用。

    2)你也可以使用 MultiBinding 去拼接字符串。

    看完资料,结合你的需求就可以完成了。


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年3月10日 3:24
    版主
  • 我想不用绑定StringFormat这之类的,能不能在XAML里面直接进行字符串的一些操作,谢谢
    2011年3月10日 4:36
  • 哈,中午试出了一招

    <Window x:Class="T_Mar10.MainWindow"
    		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:sys="clr-namespace:System;assembly=mscorlib"
    		Title="MainWindow" Height="350" Width="525">
    	<Window.Resources>
    		<ObjectDataProvider x:Key="strcat"
    							ObjectType="{x:Type sys:String}"
    							MethodName="Concat">
    			<ObjectDataProvider.MethodParameters>
    				<sys:String>aaa</sys:String>
    				<sys:String>bbb</sys:String>
    				<sys:String>ccc</sys:String>
    			</ObjectDataProvider.MethodParameters>
    		</ObjectDataProvider>
    	</Window.Resources>
    	<Grid>
    		<Label>
    			<Label.Content>
    				<Binding Source="{StaticResource strcat}"/>
    			</Label.Content>
    		</Label>
    	</Grid>
    </Window>
    2011年3月10日 6:12
  • @ AceBear 感谢你提供这个新的思路,很好。

    @ 王晶晶 首先,你要先明白一点,在XAML里面是不能进行任何逻辑运算的,WPF设计的初衷就是这样,因为用WPF更加推荐UI设计和数据逻辑分开。我觉得AceBear这个方法就不错。还有就是,比方其他的Convert之类的方法,其实都是在后台的代码中完成的,你可以用Convert去完成各中字符串的拼接。


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年3月10日 6:21
    版主
  • 其实我用到字符串拼接的地方很简单,如果每一个都用后台的Convert,我觉得比较麻烦,所以我觉得简单的字符串直接在前台做最好了。
    2011年3月10日 8:17
  • 如果你就是要简单的拼接,那你完全可以用TextBlock,而不是Label,这样你可以用Run去拼接字符串在TextBlock中,而且不同的Run可以应用不同的字体格式,更加方便。比如:

    <TextBlock>

        <Run>A</Run>

        <Run>B</Run>

    </TextBlock>

     

     

     


    Sheldon _Xiao[MSFT]

    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手

    2011年3月10日 10:23
    版主
  • 嗯,谢谢!我需要的就是这样的。
    2011年3月11日 1:50
  • 那你就要换成TextBlock了,参考它的Inlines属性。

    如果是你想要的,别忘“标记为答复”!


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年3月11日 1:53
    版主