【问题标题】:WPF MVVM bind Margin property of a controlWPF MVVM 绑定控件的 Margin 属性
【发布时间】:2017-06-12 07:17:22
【问题描述】:

使用 caliburn.micro 以 MVVM 模式构建 wpf 应用程序。 我需要动态设置按钮的边距属性。 有一个question,我有一些想法,但不适用于 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);

我看不到应用到 Button 的边距。它的默认边距为 0。

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

【问题讨论】:

  • “没有成功”是什么意思?您是否在“输出”窗口中发现任何错误?
  • 如果您的 VM 中有 Thickness,那么我将在此解决方案中质疑您的 MvvM。但是您需要一个 Style 来处理 Button 中的 Margin 和 VM 上的属性,也许是 bool,但这取决于您的要求。
  • 为什么要在VM中做呢?边距纯粹与 UI 相关,将其放在 XAML.cs 文件中没有害处。
  • @VimalCK 我的意思是从不更新其源属性的绑定上设置 Mode=TwoWay 完全没有意义
  • 这样的属性是否应该在视图模型中是一个见仁见智的问题。没有技术上的理由不把它放在那里。但是,将 Mode=TwoWay 设置为非工作绑定的可能解决方案是毫无意义的。

标签: c# wpf mvvm caliburn.micro


【解决方案1】:

这是一个ButtonMarginGrid 中工作的示例。
这是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,或者您必须在您的项目中引用一个程序集。
假设
我假设您的保证金只需要一个新值。
更新
根据您的要求,您需要将值保留在 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;

【讨论】:

  • 如何在运行时更改边距?
  • 你的意思是Margin的值吗?所以在运行时你想把它从 (10,10,10,10) 改成 (20,10,20,10)?
  • 是的,我需要改变,至少第一次我需要根据一些逻辑来设置它。我的想法是在 ViewModel 构造函数中设置值。
  • 好的,您的保证金是否取决于屏幕尺寸?如果是这样,还有其他方法可以实现此功能。 View Model 不应该确定 Margin,因为它纯粹是 Views 问题。
  • 不,没有什么取决于屏幕大小。我只想设置边距。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
相关资源
最近更新 更多