【发布时间】:2014-09-28 09:23:42
【问题描述】:
嗨,我完全是 WPF 新手,我希望能够通过 CS 文件中的变量控制 XAML 中任何内容的边距值,我已经阅读了一些关于 stackoverflow 的问题/答案,试图实现它,但它似乎不起作用。
这是我迄今为止尝试过的,但我无法让它以某种方式工作,真的需要就此提出建议。
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Thickness _Margin = new Thickness(100, 20, 0, 0);
public Thickness Margin
{
get { return _Margin; }
set
{
_Margin = value;
//Notify the binding that the value has changed.
this.OnPropertyChanged("Margin");
}
}
public MainWindow()
{
InitializeComponent();
No.DataContext = _Margin;
}
protected void OnPropertyChanged(string strPropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="No">
<Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="{Binding _Margin}" Name="border1" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
【问题讨论】: