簡體   English   中英

WPF中的網格(或控件)的綁定邊距

[英]Binding margin of a grid (or a control) in WPF

我想綁定一些控件的邊距,比如說一個按鈕:

<Window.Resources>
    <local:MarginConverter x:Key="marginConverter1"/>
</Window.Resources>

<Grid HorizontalAlignment="Left" VerticalAlignment="Top" 
      Margin="{Binding MyThickness, 
      Converter={StaticResource marginConverter1}}">
    <Button>Button1</Button>
</Grid>

根據參考。 在這里: SO:綁定部分頁邊距 ,我創建了MarginConverter類和MyDataContext類來實現INotifyPropertyChanged接口(請參見下文),但是Button1保留在左上角位置(好像其頁邊距為0)。

Public Class MyDataContext
   Implements INotifyPropertyChanged

   Private _myThickness As Thickness = New Thickness(20, 10, 20, 0)

   Public Event PropertyChanged As PropertyChangedEventHandler _
       Implements INotifyPropertyChanged.PropertyChanged

   Private Sub OnPropertyChanged(propertyName As String)
       RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
   End Sub

   Public Property MyThickness As Thickness
       Get
           Return _myThickness 
       End Get
       Set(value As Thickness)
           _myThickness = value
           OnPropertyChanged("MyThickness")
       End Set
   End Property
End Class

以及后面的代碼:

Dim myDataContext1 As New MyDataContext()
Private Sub Window1_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
   myDataContext1.MyThickness = New Thickness(20, 150, 20, 0)
End Sub

請幫助我澄清我的誤解,即使是基礎知識或您的清晰解釋也將不勝感激!

P / S: 我打算綁定上邊距的目的是當用戶執行某些特定任務時,窗口頂部將出現25個高度的邊框,因此所有現有控件都必須放下。 因此,如果您有其他方法,請在此處分享。 謝謝。

如果只想動態地在網格頂部提供25的高度,則可以通過在網格頂部添加一個邊框並將其可見性從“ Collapsed”更改為“ visible”來實現。

<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
    <StackPanel>
    <Border Height="25"   Visibility="Collapsed">

    </Border>
    <Button >Button1</Button>
    </StackPanel>
</Grid>

好的,這里是您的工作示例,很抱歉,但僅適用於C#app.xaml

  <Application.Resources>
    <ResourceDictionary>
            <viewModel:TestVM x:Key="TestVm"/>
    </ResourceDictionary>
  </Application.Resources>

視圖模型

public class TestVM : INotifyPropertyChanged
    {
        Thickness myThickness = new Thickness(20,10,20,0);

        public Thickness MyThickness
        {
            get { return myThickness; }
            set { myThickness = value; OnPropertyChanged(); }
        }




        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Window1.xaml

<Window x:Class="WPF_Test_Canvas_Chart.Windows.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Test_Canvas_Chart.Windows"
        mc:Ignorable="d"
        DataContext="{StaticResource TestVm}"
        Title="Window1" Height="300" Width="300">
    <Grid HorizontalAlignment="Left" VerticalAlignment="Top" 
      Margin="{Binding MyThickness}">
            <Button>Button1</Button>
        </Grid>

</Window>

Window1.cs

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            var data = this.DataContext as TestVM;
            data.MyThickness = new Thickness(100,10,20,0);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM