WPF MVVM绑定控件的边际属性

WPF MVVM绑定控件的边际属性

问题描述:

使用caliburn.micro在MVVM模式下构建wpf应用程序。 我需要动态设置按钮的边距属性。 有一个question,我有一些想法,但不是MVVM。WPF MVVM绑定控件的边际属性

我试过这个。

XAML

<Grid> 
<Button Content="Test" Margin="{Binding ButtonMargin}"/> 
<Grid/> 

视图模型

private Thickness _buttonMargin 
public Thickness ButtonMargin 
{ 
    get { return _buttonMargin; } 
    set 
    { 
     if (_buttonMargin != value) 
     { 
      _buttonMargin = value; 
      NotifyOfPropertyChange(() => ButtonMargin); 
     } 
    } 
} 


//constructor 
ButtonMargin = new Thickness(20,10,20,10); 

我看不到应用按钮保证金。它的默认保证金为0.

这是正确的方法吗?我怎样才能做到这一点?

+0

什么是“没有成功”呢?您是否在输出窗口中观察到任何错误? – dymanoid

+0

更具体。提供可靠地再现问题的良好[mcve]。 _描述问题。精确地解释代码的作用,你希望它做什么,以及具体的_你试图解决它,以及具体是什么 - 你仍然很难找出答案。 –

+0

将xaml中的绑定模式更改为TwoWay –

以下是在Grid内工作的ButtonMargin的示例。
这里是一个pure XAML解决方案:

<UserControl x:Class="SO_app.ForRahul" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:SO_app" 
     xmlns:vm="clr-namespace:VM;assembly=VM"//reference to our View Model assembly 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.DataContext> 
    <vm:MainViewModel/>//setting up the data context of our UserControl 
</UserControl.DataContext> 
<UserControl.Resources> 
    <Style TargetType="ToggleButton">//this is the money maker, this style relies on the property of a button but can be easily adapted to Binding with a View Model 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" Value="true"> 
       <Setter Property="Margin" Value="10"/>//sets the margin of a control, you can also put in here "10,10" or "10,10,20,20" 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 
<Grid> 
    <ToggleButton Content="Test" />//Button that we modify the Margin 
</Grid> 


这里是你如何在主窗口中使用它的一个例子:

<Window x:Class="SO_app.MainWindow" 
    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:vm="clr-namespace:VM;assembly=VM" 
    xmlns:local="clr-namespace:SO_app"//this is where our Control resides 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <vm:MainViewModel/> 
</Window.DataContext> 
<Grid> 
    <Control> 
     <Control.Template> 
      <ControlTemplate> 
       <local:ForRahul/> 
      </ControlTemplate> 
     </Control.Template> 
    </Control> 
</Grid> 


如果您需要任何更多的信息让我们知道。
如果您使用的是Style,那么您可以在应用程序中重新使用它,如果您在代码中使用它,则必须将其作为static,否则必须在项目中引用程序集。
假设
我假设您只需要一个新的价值为您的保证金。
UPDATE
您的需求,你需要在XAML某处保持值我建议App.xaml,因为这是所有的窗口将默认加载,在那里你应该有像这样对它的引用:

<Thickness x:Key="btnMargin" Left="10" Right="10" Top="10" Bottom="10"/> 

然后,当你需要根据你使用它后面的代码中一些逻辑来改变它:

var newMargin = App.Current.Resources["btnMargin"] as Thickness;//get the thickness 
var button = grid.Children.OfType<Button>().FirstOrDefault(b => b.Content == "Some Text");//find the correct button 
button.Margin = newMargin; 
+0

如何在运行期间更改边距? – Rahul

+0

你是指保证金的价值吗?所以在运行时你想改变它从(10,10,10,10)到(20,10,20,10)? – XAMlMAX

+0

是的,我需要改变,至少第一次我需要根据一些逻辑来设置它。我的想法是在ViewModel构造函数中设置值。 – Rahul