将类级布尔值绑定到WPF“IsChecked”属性
我有一个VB.NET WPF应用程序,它分别以两种不同的模式运行:设计模式和评估模式。我添加了一个菜单选项和切换按钮,以便用户可以轻松选择他/她正在使用的模式。当然,整个应用程序的行为也会有所不同,这取决于它是否处于设计模式,因此创建一个类级布尔值来表示在设计模式下工作,并将这些项绑定到它的IsChecked属性是有意义的 首先,我尝试在代码中添加绑定,如下所示:将类级布尔值绑定到WPF“IsChecked”属性,wpf,vb.net,xaml,data-binding,Wpf,Vb.net,Xaml,Data Binding,我有一个VB.NET WPF应用程序,它分别以两种不同的模式运行:设计模式和评估模式。我添加了一个菜单选项和切换按钮,以便用户可以轻松选择他/她正在使用的模式。当然,整个应用程序的行为也会有所不同,这取决于它是否处于设计模式,因此创建一个类级布尔值来表示在设计模式下工作,并将这些项绑定到它的IsChecked属性是有意义的 首先,我尝试在代码中添加绑定,如下所示: Class MainWindow Private IsInDesignMode As Boolean = True
Class MainWindow
Private IsInDesignMode As Boolean = True
Private Sub Window_Loaded(sender as Object, e as RoutedEventArgs)
SetUpDataBindings()
End Sub
Private Sub SetUpDataBindings()
tglDesignMode.IsChecked = New Binding(IsInDesignMode)
mnuDesignMode.IsChecked = New Binding(IsInDesignMode)
End Sub
End Class
然后我读到控件只能绑定到属性,我读到的所有示例都显示了公共属性,所以我将上面的第2行改为:
Public Property IsInDesignMode as Boolean = True
但在这两种情况下,按钮和菜单在启动时都没有被选中,也没有同时保持选中/取消选中状态。所以我尝试了一种不同的策略,使用XAML:
<ToggleButton Name="tbnDesignMode"
IsChecked="{Binding IsInDesignMode}">
DESIGN MODE
</ToggleButton>
那也没用。我还看到一些人使用自定义转换器,但他们没有解释转换器在做什么。从C到VB的转换也没有帮助
长话短说,有人能帮我设置从这些控件的IsChecked属性到类级布尔值的绑定,并解释为什么以及如何绑定吗?参考下面的代码
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
<ToggleButton Name="tbnDesignMode"
IsChecked="{Binding Path=IsInDesignMode,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}">
DESIGN MODE
</ToggleButton>
<MenuItem Name="mnuDesignMode"
Header="_Design Mode"
IsCheckable="True"
IsChecked="{Binding Path=IsInDesignMode,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"/>
</StackPanel>
但我建议您寻找MVVM方法,而不是代码隐藏。谢谢您的帮助。我也查阅了更多关于实现MVVM的内容,并发现了更多的方法。在这种情况下,数据绑定这些属性就更有意义了。我开始更多地阅读您的代码,以便能够在其他地方重新实现它,我还有两个问题。1-XAML中RelativeSource的语法是什么?2-我假设引发新事件的代码是这样的:当属性更改时,窗口可以更新其内容;即使窗口中没有XAML或VB中的代码,这是否正确?我建议您使用WPF中的数据绑定,它将回答您的所有问题。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
<ToggleButton Name="tbnDesignMode"
IsChecked="{Binding Path=IsInDesignMode,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}">
DESIGN MODE
</ToggleButton>
<MenuItem Name="mnuDesignMode"
Header="_Design Mode"
IsCheckable="True"
IsChecked="{Binding Path=IsInDesignMode,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"/>
</StackPanel>
Imports System.ComponentModel
Class MainWindow
Implements INotifyPropertyChanged
Private isInDesignModeValue As Boolean
Public Property IsInDesignMode() As Boolean
Get
Return isInDesignModeValue
End Get
Set(ByVal value As Boolean)
isInDesignModeValue = value
OnPropertyChanged1("IsInDesignMode")
End Set
End Property
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
IsInDesignMode = True
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged1(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class