简体   繁体   English

WPF中的网格(或控件)的绑定边距

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

I want to bind some controls' margin, let's say, a button for example: 我想绑定一些控件的边距,比如说一个按钮:

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

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

According to the ref. 根据参考。 here: SO: Binding a part of the margin , I created a MarginConverter class and a MyDataContext class to implement INotifyPropertyChanged interface (see below), but the Button1 persists in the top-left position (as if its margin is 0). 在这里: 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

And the code behind: 以及后面的代码:

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

Please help me to clarify my misunderstanding, even a basic knowledge or a clear explanation from you will be appreciated! 请帮助我澄清我的误解,即使是基础知识或您的清晰解释也将不胜感激!

P/S: My intention for binding the top margin is when user does some specific task, a 25-height border will be appear at the top of the window, so all the existing controls must go down. P / S: 我打算绑定上边距的目的是当用户执行某些特定任务时,窗口顶部将出现25个高度的边框,因此所有现有控件都必须放下。 So that if you have another approach, please share here. 因此,如果您有其他方法,请在此处分享。 Thank you. 谢谢。

If you only want to provide a height of 25 on top of your grid dynamically, You can do it by adding a border on top row of the grid and change its visibility to "Collapsed" to "visible". 如果只想动态地在网格顶部提供25的高度,则可以通过在网格顶部添加一个边框并将其可见性从“ Collapsed”更改为“ visible”来实现。

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

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

Ok so here working example for you, sorry but only at C# app.xaml 好的,这里是您的工作示例,很抱歉,但仅适用于C#app.xaml

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

ViewModel 视图模型

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 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 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