刚接触WPF 我在用WPF做一个聊天程序 想实现类似QQ的效果 
登陆后 移除用户和密码的输入框 在原窗口上添加一个新的<Grid Name="abc"><ListBox></ListBox></Grid> 各位高手帮帮忙 先谢谢了

解决方案 »

  1.   

    原窗口?那么你把不同界面设计成不同的用户控件,默认(设计时)加入第一个,之后再Remove掉第一个,Add加入new出来的第二个。
      

  2.   

    你是指重新定义它的外观吧。试试使用<ItemTemplate>和<Template>。
      

  3.   

    <Grid Name="g1">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="124,110,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <PasswordBox Height="23" HorizontalAlignment="Left" Margin="126,160,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="360,127,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>        private void button1_Click(object sender, RoutedEventArgs e)
            {
                g1.Children.Clear();
                Grid _grid = new Grid(){ Name="abc"};
                ListBox _listbox=new ListBox();
                _grid.Children.Add(_listbox);
                g1.Children.Add(_grid);
            }
      

  4.   

    谢谢各位! 请问 ,我的新界面比较复杂:一个一个Add太麻烦了,有没有简单的方法,可以直接传入xaml吗?
    <Grid>
            <ListBox Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Auto" MouseLeftButtonDown="ListBox_MouseLeftButtonDown">
          
                <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 03_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Marcus</TextBlock>
                </StackPanel>
                <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 02_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Fergus</TextBlock>
                </StackPanel>
                <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 04_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Jay</TextBlock>
                </StackPanel>
                <DockPanel MouseLeftButtonDown="DockPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 03_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Marcus</TextBlock>
                </DockPanel>
              
            </ListBox>
        </Grid>
      

  5.   

    <UserControl x:Class="WpfApplication3.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <ListBox Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Auto" MouseLeftButtonDown="ListBox_MouseLeftButtonDown">            <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 03_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Marcus</TextBlock>
                </StackPanel>
                <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 02_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Fergus</TextBlock>
                </StackPanel>
                <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 04_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Jay</TextBlock>
                </StackPanel>
                <DockPanel MouseLeftButtonDown="DockPanel_MouseLeftButtonDown">
                    <Image Source="Img\Mask 03_32x32.png" />
                    <TextBlock VerticalAlignment="Center">Marcus</TextBlock>
                </DockPanel>        </ListBox>
        </Grid></UserControl>
       private void button1_Click(object sender, RoutedEventArgs e)
            {
                g1.Children.Clear();
                g1.Children.Add(new UserControl1());
            }
      

  6.   

              string dd = @"
    <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
       <ListBox Grid.Column='1' ScrollViewer.VerticalScrollBarVisibility='Auto'>
         
      <StackPanel Orientation='Horizontal' >
       <Image Source='Img\Mask 03_32x32.png' />
       <TextBlock VerticalAlignment='Center'>Marcus</TextBlock>
       </StackPanel>
       <StackPanel Orientation='Horizontal' >
       <Image Source='Img\Mask 02_32x32.png' />
       <TextBlock VerticalAlignment='Center'>Fergus</TextBlock>
       </StackPanel>
       <StackPanel Orientation='Horizontal' >
       <Image Source='Img\Mask 04_32x32.png' />
       <TextBlock VerticalAlignment='Center'>Jay</TextBlock>
       </StackPanel>
       <DockPanel >
       <Image Source='Img\Mask 03_32x32.png' />
       <TextBlock VerticalAlignment='Center'>Marcus</TextBlock>
       </DockPanel>
         
      </ListBox>
       </Grid>";
                System.IO.MemoryStream _dd = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(dd));            
                Grid _Grid = (Grid)XamlReader.Load(_dd);
                g1.Children.Clear();
                g1.Children.Add(_Grid);
      

  7.   

    传入并且解析xmal简单,New并且Add用户控件到容器复杂?我晕!
      

  8.   

    其实对于wpf来说,你没有必要非要挤占“原来的窗口”。用不同的窗口来实现不同的功能,这样更好。
      

  9.   

    用的Add(New Control()) 谢谢 
    借此帖想再问个问题  关于<image>数据绑定       public string FacePath
            {
                get { return String.Format(@"face\{0}.bmp", faceId); }
                set { facePath = value; }
            }        private int faceId;        public int FaceId
            {
                get { return faceId; }
                set { faceId = value; }
            }得到的FacePath是face\\66.bmp
    怎么是双斜杠????
      

  10.   

    faceId的值是66  而且String.Format(@"face{0}.bmp", faceId); 这样去掉斜杠的值是face66.bmpString.Format(@"face\{0}.bmp", faceId)这样的是face\\66.bmp