C# WPF中网格(或控件)的绑定边界

C# WPF中网格(或控件)的绑定边界,c#,wpf,vb.net,binding,margin,C#,Wpf,Vb.net,Binding,Margin,我想绑定一些控件的边距,比如一个按钮: <Window.Resources> <local:MarginConverter x:Key="marginConverter1"/> </Window.Resources> <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="{Binding MyThickness, Converter={Sta

我想绑定一些控件的边距,比如一个按钮:

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

<Grid HorizontalAlignment="Left" VerticalAlignment="Top" 
      Margin="{Binding MyThickness, 
      Converter={StaticResource marginConverter1}}">
    <Button>Button1</Button>
</Grid>
以及背后的代码:

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的高度, 您可以通过在网格的顶行添加边框并将其可见性更改为“折叠”到“可见”来完成此操作


按钮1

如果您只想在网格顶部动态提供25的高度, 您可以通过在网格的顶行添加边框并将其可见性更改为“折叠”到“可见”来完成此操作


按钮1

好的,这里有一个为您提供的示例,很抱歉,但仅限于C# app.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>

好的,这里是你们的工作示例,抱歉,但只在C# app.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>

我不太擅长VB,而且您没有提供
marginConverter1
代码,但看起来您正在尝试在绑定方面做一些事情,这已经是一个
厚度
,对于绑定来说已经足够了
Margin
的值为
Thickness
,因此您需要为
Thickness
类型的数据或任何其他类型的数据提供绑定。PS如果您按原样使用链接中的代码,这将不起作用,bc他们提供了带绑定的
双精度
,并使用转换器将其转换为
厚度
,而您已经提供了
厚度
。嗨,沙克拉,感谢您的回复。是的,我在给定的SO链接中使用了MarginConverter类的VB代码。我会根据你的建议设法解决的!在BindingDear@Shakra中尝试不使用
Converter
的代码,为了只使用最高边距值,我删除了xaml中的
MarginConverter
,但按钮仍保留在左上角。我还尝试添加一个名为
MyTopMargin
的新属性作为
Integer
,以再次使用转换器,但它也不起作用。我认为问题出在其他地方。。。你能想出来吗?我不太擅长VB,你也没有提供
marginverter1
代码,但看起来你正在尝试做一些关于绑定的事情,这已经是一个
厚度
,对于绑定来说已经足够了
Margin
的值为
Thickness
,因此您需要为
Thickness
类型的数据或任何其他类型的数据提供绑定。PS如果您按原样使用链接中的代码,这将不起作用,bc他们提供了带绑定的
双精度
,并使用转换器将其转换为
厚度
,而您已经提供了
厚度
。嗨,沙克拉,感谢您的回复。是的,我在给定的SO链接中使用了MarginConverter类的VB代码。我会根据你的建议设法解决的!在BindingDear@Shakra中尝试不使用
Converter
的代码,为了只使用最高边距值,我删除了xaml中的
MarginConverter
,但按钮仍保留在左上角。我还尝试添加一个名为
MyTopMargin
的新属性作为
Integer
,以再次使用转换器,但它也不起作用。我认为问题出在其他地方。。。你能想出来吗?Prasanth,你从另一个角度看了我一眼,但效果不错!非常感谢!Prasanth,你从另一个角度看了我一眼,但效果不错!非常感谢!嗨,沙克拉。我发现很难实现我现有的VB项目。但非常感谢你的深入调查!嗨,沙克拉。我发现很难实现我现有的VB项目。但非常感谢你的深入调查!
  <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));
        }
    }
<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>
public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            var data = this.DataContext as TestVM;
            data.MyThickness = new Thickness(100,10,20,0);
        }
    }