比如我原来的字符串为。你好啊我是一个程序员你也是一个程序员吧。现在要把所有的程序员几个字变为红色
谢谢

解决方案 »

  1.   

    改变颜色GDI画出这几个字最好了
      

  2.   

    如果是在不同的textblock里,只要设置对应的textblock的forecolor就行了啊
      

  3.   

    哦。这样是不行的。我只能在一个textbox里面。还有其他的方法吗?
    改变颜色GDI画出这几个字最好了给个例子呢。
      

  4.   

    Font f=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string str="";
    if (richTextBox1.Find(str)>0)
    {
    int pos=richTextBox1.Find(str);
    richTextBox1.SelectionStart=pos;
    richTextBox1.SelectionLength=str.Length;   
    richTextBox1.SelectionFont=f;
    richTextBox1.SelectionColor=Color.Red;
    }   
      

  5.   

    wpf中的richtextbox没有selectionstart和selectionfont属性啊?请问怎么设置?
      

  6.   

    LZ说说为什么不能用多个textblock,我感觉好像没有什么情况不能这么用的吧,如果是同一个字符串,可以同时绑定到几个textblock然后加不同的converter使其中显示不同的子字符串
      

  7.   

    我是动态添加的啊。根据不同的情况添加不同的信息。这些信息没什么规律性。只是在有相同的字符串的时候那个字符串要变色。而且出现得字符串的个数也不一样的。有的可能有10个。有的可能是几千个。怎么用多个teztblock??
      

  8.   

    动态添加啊可以在convert里把string转成不定数量的textblock,如果是和别的string有关可以多重绑定
      

  9.   

    布局是容器的问题,看你用什么容器,其实WPF绑定已经可以实现很大部分的应用,关键看怎么用
      

  10.   

    这样对我这个需求来说实在是太烦了。难道就没有简单的路走么。用多个textblock我是不会采用的,谢谢楼上的。
      

  11.   

    何来麻烦那干脆不要用WPF了浪费啊
      

  12.   

     <Controls:MaskTextBox MaskType="Digits" x:Name="txtOpenNo"  Width="150"
                   Text="{Binding Path=CurrentActivity.OpenprizeNo}"  Margin="15 0 0 0" 
                                      />
      

  13.   

    WPF 的xaml 和html 不同,
    所以就吧能像 html样 一个string 再 inner 就ok
    我原来是在cs代码里面 判断,再加到textblock ,
     TextBlock txt = new TextBlock();
     txt.Inlines.Add()
    想加啥就加啥
    几段 控件加起来现在没有在代码里面写了
    但是实现也差不多
    就是在xaml 里面 DataTemplate 
    再选样式
      

  14.   

    Converter:
    public class mStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string s = (string) value;
                string innerString = (string) parameter;
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                while (s.IndexOf(innerString) > 0)
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = s.Substring(0, s.IndexOf(innerString));
                    s = s.Substring(s.IndexOf(innerString));
                    sp.Children.Add(tb);
                    tb = new TextBlock();
                    tb.Text = innerString;
                    tb.Foreground = Brushes.Red;
                    s = s.Substring(innerString.Length);
                    sp.Children.Add(tb);
                }
                TextBlock t = new TextBlock();
                t.Text = s;
                sp.Children.Add(t);
                return sp;
            }        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new System.NotImplementedException();
            }
    界面代码
        <Window.Resources>
            <mStringTest:mStringConverter x:Key="stringConverter" />
        </Window.Resources>
        <Grid Name="RootLayOut">
            <ContentControl>
                <Binding Converter="{StaticResource stringConverter}" ConverterParameter="程序员" />
            </ContentControl>
        </Grid>
    后台绑定
    RootLayOut.DataContext = "你好啊我是一个程序员你也是一个程序员";
      

  15.   

    <TextBlock Padding="10,10,10,10" LineHeight="25" Grid.Column="1" FontSize="16" TextWrapping="Wrap" Foreground="White" Margin="8,10,10,53" Background="#FF4688C0">
                    <TextBlock.Effect>
                        <DropShadowEffect/>
                    </TextBlock.Effect>
                    <Bold>黑体</Bold><LineBreak />  正常
              <Run FontWeight="Bold" Foreground="Yellow" Text="黄色的字"/>正常。
     </TextBlock>