【问题标题】:Binding with for grid Column与网格列绑定
【发布时间】:2015-07-15 14:29:38
【问题描述】:

我有一个 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

【问题讨论】:

    标签: vb.net binding grid width


    【解决方案1】:

    主要问题是ColumnDefinition.Width 被键入为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>
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 2011-06-24
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多