与网格列绑定
Binding with for grid Column
我有一个包含 3 列的网格。我希望当你点击一个按钮时,第一列改变宽度。
这是代码,但不起作用。为什么?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width= "{Binding Path=PositionCol,Mode=TwoWay}" x:Name="column1"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>
<Button Content="Button" Grid.Column="2" HorizontalAlignment="Left" Height="38" Margin="4,4,4,4" VerticalAlignment="Top" Width="55" Click="Button_Click"/>
</Grid>
Imports System.ComponentModel
Public Class Window1
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _PositionCol As Double
Public Property PositionCol As Double
Get
Return _PositionCol
End Get
Set(value As Double)
_PositionCol = value
OnPropertyChanged("PositionCol")
End Set
End Property
Private Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
PositionCol = 70
End Sub
End Class
主要问题是 ColumnDefinition.Width
is typed as GridLength
,而不是 Double
。更改您的 属性:
Private _PositionCol As GridLength = New GridLength(200)
Public Property PositionCol As GridLength
Get
Return _PositionCol
End Get
Set(value As GridLength)
_PositionCol = value
OnMyPropertyChanged("PositionCol")
End Set
End Property
' ...
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
PositionCol = New GridLength(70)
End Sub
此外,请确保您在构造函数中设置 DataContext
:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.DataContext = Me
End Sub
您可能还想对列定义进行一些调整:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Path=PositionCol,Mode=TwoWay}"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
我有一个包含 3 列的网格。我希望当你点击一个按钮时,第一列改变宽度。
这是代码,但不起作用。为什么?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width= "{Binding Path=PositionCol,Mode=TwoWay}" x:Name="column1"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>
<Button Content="Button" Grid.Column="2" HorizontalAlignment="Left" Height="38" Margin="4,4,4,4" VerticalAlignment="Top" Width="55" Click="Button_Click"/>
</Grid>
Imports System.ComponentModel
Public Class Window1
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _PositionCol As Double
Public Property PositionCol As Double
Get
Return _PositionCol
End Get
Set(value As Double)
_PositionCol = value
OnPropertyChanged("PositionCol")
End Set
End Property
Private Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
PositionCol = 70
End Sub
End Class
主要问题是 ColumnDefinition.Width
is typed as GridLength
,而不是 Double
。更改您的 属性:
Private _PositionCol As GridLength = New GridLength(200)
Public Property PositionCol As GridLength
Get
Return _PositionCol
End Get
Set(value As GridLength)
_PositionCol = value
OnMyPropertyChanged("PositionCol")
End Set
End Property
' ...
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
PositionCol = New GridLength(70)
End Sub
此外,请确保您在构造函数中设置 DataContext
:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.DataContext = Me
End Sub
您可能还想对列定义进行一些调整:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Path=PositionCol,Mode=TwoWay}"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>