C#WPF .xaml控制数据绑定问题。 [英] C# WPF .xaml control data-binding question.

查看:141
本文介绍了C#WPF .xaml控制数据绑定问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个问题。我有一个带有属性的用户对象。我有一个登录屏幕,当用户正确登录时,我将用户对象传递到下一个窗口。在下一个窗口的顶部,我想在控件中显示新行。我应该使用文本块还是2个标签?

我可以使用控件中的user.Username来解决这个问题,但我想知道我是否应该使用Data Binding这样做,因为我使用的是WPF C#窗口。谢谢。

'你好:

用户名'



  //  将用户对象传递到下一个窗口的登录窗口 
尝试 {
loginUser = SQLuserAccess.UserLogin(txtUsername.Text,txtPassword.Password.ToString());
if (txtUsername.Text == loginUser.Username&& txtPassword.Password == loginUser.Password){
if (loginUser.IsAdmin == true ){
Window showAdminSrc = new AdminWindow(loginUser);
showAdminSrc.Show();
关闭();
} else if (loginUser.IsAdmin == false ){
Window nonAdmin = new CustomerScreen(loginUser);
nonAdmin.Show();
关闭();
} else
lblInvalidText.Content = 管理员不是真或假!;
}
// 管理窗口屏幕,其中包含对象
public AdminWindow(用户用户){
adminUser = user;
InitializeComponent();
// 在窗口上显示特定用户名。
TextBlockName.Text = Hello: + Environment.NewLine + user.Username;
}
// .xaml中的控件< TextBlock x:Name =TextBlockName TextWrapping =WrapHorizo​​ntalAlignment =RightMargin =0,30,35,0FontSize =16> Hello:<! - < LineBreak />< Run Text ={Binding Username }/> - >< / TextBlock>
// AND HERE有些事情我正在尝试数据绑定但尚未显示<! - 学习数据绑定文本块以显示用户对象名称。
< TextBlock x:Name = TextBlockUsername Text = {绑定用户名} Horizo​​ntalAlignment = FontSize = 16 Margin = 0,60,35,0 />
- >





我尝试过:



我通过传入一个对象并使用object.property工作,但是我应该用数据绑定来做这个,因为这是WPF。

解决方案

我应该用数据绑定做这个,因为这是WPF吗?



是。

1。 Label和Textblock之间的选择取决于要求。如果您的数据只是文本,请转到TextBlock。由于它重量轻,但如果您需要某些内容,如图像,网格或其他复杂类型,那么标签对您来说很好。就个人而言,如果需要的话,我会使用带有一些样式的文本块。



2. DataBinding是最佳选择,WPF没有MVVM就完成了。如果您不使用MVVM,则可以将逻辑移至viewModel。其次,您不必担心获取/设置。 WPF数据绑定为您做得更好。通过使用数据绑定,您可以将页面绑定到不同的viewModel,比如说一个viewModel用于设计时间,而另一个用于实际实现


Hello all,
I have a question. I have a user object with properties. I have a login screen and when the user logins correctly I pass the user object to the next window. On the top of this next window I would like to display this below with the new line in the control. Should I use a text block or maybe 2 labels?
I can get this working no problem using user.Username in the control but I was wondering should I be doing this with Data Binding because I am using a WPF C# window. Thank you.
‘Hello:
username’

//Login window which passes the user object to the next window
try{
                    loginUser = SQLuserAccess.UserLogin(txtUsername.Text, txtPassword.Password.ToString());
                    if (txtUsername.Text == loginUser.Username && txtPassword.Password == loginUser.Password) {
                        if (loginUser.IsAdmin == true) {
                            Window showAdminSrc = new AdminWindow(loginUser);
                            showAdminSrc.Show();
                            Close();
                        }  else if (loginUser.IsAdmin == false){
                            Window nonAdmin = new CustomerScreen(loginUser);
                            nonAdmin.Show();
                            Close();
                        } else
                            lblInvalidText.Content = "Admin is not True or False!";
                    }
//Adminwindow screen which takes object
        public AdminWindow(User user) {
            adminUser = user;
            InitializeComponent();
            //Display specific username on window.
            TextBlockName.Text = "Hello:" + Environment.NewLine +user.Username;
        }
//The control in .xaml        <TextBlock x:Name="TextBlockName" TextWrapping="Wrap" HorizontalAlignment="Right" Margin="0,30,35,0" FontSize="16">Hello:<!--<LineBreak /><Run Text="{Binding Username}"/>--></TextBlock>
//AND HERE IS SOMETHING I WAS TRYING DATA BINDING BUT HAVE NOT FIGURED OUT      		  <!--   LEARN DATA BINDING TEXTBLOCK TO DISPLAY USER OBJECT NAME.
        <TextBlock x:Name="TextBlockUsername" Text="{Binding Username}" HorizontalAlignment="Right" FontSize="16" Margin="0,60,35,0"/>
-->



What I have tried:

I have it working by passing in an object and using object.property but should i be doing this with data binding because this is WPF.

解决方案

should i be doing this with data binding because this is WPF?

Yes.


1. Choice between Label and Textblock depends on requirements. If your data is only text, go for TextBlock. As it is light weight, but if you need some content like image, grid or other complex typed, then label is fine for you. Personally for simple messages, I use textblock with some styling if needed.

2. DataBinding is best choice, WPF is incompleted without MVVM. If you are not using MVVM, its possible to move your logic to viewModel. Secondly you dont need to be worry about get/set. WPF databind doing this better for you. By use of Databinding you can bind your pages to different viewModels, let say one viewModel for design time, with other for real implemenation


这篇关于C#WPF .xaml控制数据绑定问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆