当Bool变量变为True时,更改标签 [英] Changing a Label when a Bool variable turns True
问题描述
我不太确定如何解释这个...我要把代码放在psuedo代码中以便于阅读
几乎我想要一个标签改变它的文本,当一个类的bool变量更改...我不知道我需要使用,因为我使用WPF和类不能只是改变我不认为的标签? p>
我需要某种事件吗?或WPF事件?感谢任何帮助
public MainWindow()
{
SomeClass temp = new SomeClass();
void ButtonClick(sender,EventArgs e)
{
if(temp.Authenticate)
label.Content =AUTHENTICATED;
}
}
public SomeClass
{
bool _authenticated;
public bool Authenticate()
{
//发送UDP msg
//等待UDP msg进行身份验证
//相应地返回true或false
}
}
另一个除了BradledDotNet之外的WPF方法的答案是使用触发器。这将是纯粹基于无转换代码的XAML。
XAML:
<标签>
< Label.Style>
< Style TargetType =Label>
< Style.Triggers>
< DataTrigger Binding ={Binding Path = IsAuthenticated}Value =True>
< Setter Property =ContentValue =Authenticated/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Label.Style>
< / Label>
相同的规则适用于BradledDotNet提及的ViewModel,并且IsAuthenticated属性应该引发PropertyChanged事件。
- 按照Gayot Fow的建议添加一些样板代码 -
查看代码后面:
为了简单起见,我将只是将视图的DataContext设置在后面。为了更好的设计,您可以使用ViewModelLocator,如 here 。
public partial class SampleWindow:Window
{
SampleModel _model = new SampleModel();
public SampleWindow(){
InitializeComponent();
DataContext = _model; //将model / viewmodel附加到DataContext以在XAML中绑定
}
}
示例模型(应该是ViewModel):
这里的要点是附加模型/ viewmodel必须实现INotifyPropertyChanged。 / p>
public class SampleModel:INotifyPropertyChanged
{
bool _isAuthenticated = false;
public bool IsAuthenticated {
get {return _isAuthenticated; }
set {
if(_isAuthenticated!= value){
_isAuthenticated = value;
OnPropertyChanged(IsAuthenticated); //提高此事件是绑定正常工作的关键
}
}
}
公共事件PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName){
if(PropertyChanged!= null){
PropertyChanged(this,new PropertyChangedEventArgs(propName));
}
}
}
i'm not really sure how to explain this... i'm going to put the code in psuedo code for easy reading
pretty much I want a label to change it's text when a bool variable of a class changes... i'm not sure what I need to use since i'm using WPF and the class can't just change the label i don't think?
do i need some sort of event? or a WPF event? thanks for any help
public MainWindow()
{
SomeClass temp = new SomeClass();
void ButtonClick(sender, EventArgs e)
{
if (temp.Authenticate)
label.Content = "AUTHENTICATED";
}
}
public SomeClass
{
bool _authenticated;
public bool Authenticate()
{
//send UDP msg
//wait for UDP msg for authentication
//return true or false accordingly
}
}
Another WPF approach other than BradledDotNet answer is to use triggers. This will be purely XAML based without converter code.
XAML:
<Label>
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsAuthenticated}" Value="True">
<Setter Property="Content" Value="Authenticated" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Same rule applies as mentioned by BradledDotNet, ViewModel should be attached and "IsAuthenticated" property should raise the PropertyChanged event.
-- Add some boilerplate code as suggested by Gayot Fow --
View Code Behind:
For simplicity I will just set the DataContext of the view in code behind. For a better design you can use ViewModelLocator as explained here.
public partial class SampleWindow : Window
{
SampleModel _model = new SampleModel();
public SampleWindow() {
InitializeComponent();
DataContext = _model; // attach the model/viewmodel to DataContext for binding in XAML
}
}
Example Model (it should really be ViewModel):
The main point here is the attached model/viewmodel has to implement INotifyPropertyChanged.
public class SampleModel : INotifyPropertyChanged
{
bool _isAuthenticated = false;
public bool IsAuthenticated {
get { return _isAuthenticated; }
set {
if (_isAuthenticated != value) {
_isAuthenticated = value;
OnPropertyChanged("IsAuthenticated"); // raising this event is key to have binding working properly
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
这篇关于当Bool变量变为True时,更改标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!