Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
anh cho em hỏi về add và remove trong Gird của WPF????
15:23 22-01-2018
442 lượt xem
4 bình luận
09:55 25-01-2018
em có 1 from main như vậy.
<Window x:Class="kho.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CoffeeShop" Height="350" Width="525" WindowStartupLocation="CenterScreen" WindowState="Maximized" xmlns:my="clr-namespace:kho" Closing="Window_Closing" Loaded="Window_Loaded">
<Grid Background="#8b92c6" Loaded="Grid_Loaded" Name="G_main">
</Grid>
</Window>
còn này usercontrol
<UserControl x:Class="kho.USlogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="560" Loaded="UserControl_Loaded">
<UserControl.Resources>
<ResourceDictionary Source="Resource/RDlogin.xaml"></ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid Style="{StaticResource G_login}">
<Border Style="{StaticResource BD_Login}">
<Grid Style="{StaticResource BG_login}">
<Label Style="{StaticResource LB_login_TD}"
Content="Đăng Nhập Hệ Thống"
Margin="0,-1,0,0">
</Label>
<Label Style="{StaticResource LB_DNMK_login}"
Margin="152,77,278,143"
Content="Tên Đăng nhập"></Label>
<Label Style="{StaticResource LB_DNMK_login}"
Content="Mật Khẩu" Margin="152,123,278,99"/>
<TextBox Style="{StaticResource TXT_Login}"
Name="txt_user"
Margin="0,82,68,139">
</TextBox>
<PasswordBox Style="{StaticResource PWB_Login}" Name="txt_pass"
Margin="288,123,68,98"/>
<Image Name="im_login" Style="{StaticResource IMG_Login}"
Margin="23,77,437,73">
</Image>
<Button Name="bt_login" Style="{StaticResource bt_login}"
Content="Đăng nhập"
Margin="288,174,0,42" Click="bt_login_Click" HorizontalAlignment="Left" Width="80"></Button>
<Button Content="Thoát" Style="{StaticResource bt_login}"
Margin="0,174,68,42"
Name="bt_cancel" Click="bt_cancel_Click" HorizontalAlignment="Right" Width="80" />
</Grid>
</Border>
</Grid>
</Grid>
</UserControl>
này là code xử lý của usercontrol
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace kho
{
/// <summary>
/// Interaction logic for USlogin.xaml
/// </summary>
public partial class USlogin : UserControl
{
public USlogin()
{
InitializeComponent();
}
private void bt_cancel_Click(object sender, RoutedEventArgs e)
{
App.Current.Shutdown();
}
private void bt_login_Click(object sender, RoutedEventArgs e)
{
MainWindow fmain = new MainWindow();
USlogin lg = new USlogin();
USmanage mg=new USmanage();
fmain.G_main.Children.Remove(lg);
fmain.G_main.Children.Add(mg);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
cho em hỏi là cái phần code này nó không remove được cái usercontrol và không add thêm usercontrol khác được ak
private void bt_login_Click(object sender, RoutedEventArgs e)
{
MainWindow fmain = new MainWindow();
USlogin lg = new USlogin();
USmanage mg=new USmanage();
fmain.G_main.Children.Remove(lg);
fmain.G_main.Children.Add(mg);
}
Dạ Grid là 1 control. Z để e thử lại
Thank a nhiều ak
mà mình nhớ Grid là Controls mà nhỉ
bạn đang remove 1 thằng lg mới hoàn toàn. k phải thằng bạn đã add. 2 thằng cùng bộ vest,k cùng khuôn mặt k có nghĩa nó là 1. bạn phải:
1. lấy ra được thằng đó trong danh sách children hoặc Controls của grid.
2. khi tạo ra => add => lưu nó lại trong 1 danh sách mà sau lấy ra remove
Không dùng phương thức Remove được. Phải dùng fmain.G_main.Children.Clear()