【问题标题】:Moving an image by controlling the margin通过控制边距移动图像
【发布时间】: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>

【问题讨论】:

    标签: c# wpf image xaml margin


    【解决方案1】:

    您使用的属性名称“公共厚度边距”存在问题,它覆盖了应重命名的 Window.Margin 属性。其次,您绑定的是成员值而不是属性值。

    试试这段代码(我添加了一个按钮来触发 PropertyChanged 事件,你应该替换它任何你想使用的):

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private Thickness _margin = new Thickness(100, 20, 0, 0);
    
        public Thickness GridMargin
        {
            get { return _margin; }
            set
            {
                _margin = value;
                //Notify the binding that the value has changed.
                this.OnPropertyChanged("GridMargin");
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
    
            No.DataContext = this;
        }
    
        protected void OnPropertyChanged(string strPropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
            GridMargin = new Thickness(r.Next(0, 100));
        }
    }
    

    XAML:

    <Grid Name="No">
        <Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="{Binding GridMargin}" Name="border1" VerticalAlignment="Top" Width="200" />
    
        <Button Content="Margin" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="ButtonBase_OnClick"></Button>
    </Grid>
    

    【讨论】:

    • 谢谢,我试试。是否还有其他方法可以在不使用按钮的情况下触发属性更改?就像变量边距的值由于某些事情而改变一样。只有这样它才会更新?
    • 当然你只需要调用 GridMargin 设置器你可以使用任何东西来做它例如UI 事件:OnRenderSizeChanged、OnMouseDown、OnMouseEnter、OnMouseLeave... 或任何自定义事件、来自代码的异步调用,例如一个 HTTP 请求,当响应准备好时,你设置一个新的边距......试试这个: protected override void OnMouseWheel(MouseWheelEventArgs e) { double delta = e.Delta/100; GridMargin = new Thickness(GridMargin.Bottom + delta); }
    【解决方案2】:

    您设置数据上下文的方式不一致。 DataContext 被设置为“MainWindow”的“_Margin”属性,但随后绑定也以路径“_Margin”为目标。你需要这个:

    No.DataContext = this;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 2011-12-04
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      相关资源
      最近更新 更多