【问题标题】:Binding margin of a grid (or a control) in WPFWPF中网格(或控件)的绑定边距
【发布时间】:2017-05-11 09:10:03
【问题描述】:

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

<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: Binding a part of the margin,我创建了一个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 高的边框,因此所有现有控件都必须向下。 因此,如果您有其他方法,请在此处分享。谢谢。

【问题讨论】:

  • 我不太擅长 VB,而且您没有提供 marginConverter1 代码,但看起来您正在尝试在绑定上做一些事情,这已经是 Thickness对于绑定来说绰绰有余。 Margin 的值为Thickness,因此您需要提供与Thickness 类型的数据或任何其他类型的转换器的绑定。 PS,如果您按原样使用链接中的代码,这将不起作用,因为他们提供了带有绑定的double,并使用转换器将其转换为Thickness,而您已经提供了Thickness
  • 嗨,Shakra,感谢您的回复。是的,我在给定的 SO 链接中使用了 MarginConverter 类的 VB 代码。我会根据你的建议来解决的!
  • 在绑定中尝试不使用Converter 的代码
  • 亲爱的@Shakra,为了只有上边距值,我删除了 xaml 中的MarginConverter,但按钮仍然留在左上角。我还尝试添加一个名为MyTopMargin 的新属性为Integer 以再次使用转换器,但它也不起作用。我认为问题出在其他地方......你能弄清楚吗?

标签: c# wpf vb.net binding margin


【解决方案1】:

好的,这里是你的工作示例,对不起,但仅限于 C# 应用程序.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);
        }
    }

【讨论】:

  • 嗨沙克拉。我发现很难在我现有的 VB 项目中实施。但是非常感谢您的深入调查!
【解决方案2】:

如果您只想在网格顶部动态提供 25 的高度, 您可以通过在网格的第一行添加边框并将其可见性从“折叠”更改为“可见”来实现。

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

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

【讨论】:

  • Prasanth,你让我从不同的角度重新审视,但它有效!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2010-11-22
  • 2012-01-09
相关资源
最近更新 更多