[英]How to set a left margin only in Xamarin Forms XAML?
This question is the same as How to set a top margin only in XAML? 此问题与如何仅在XAML中设置上边距相同? but regarding Xamarin instead of WPF. 但关于Xamarin而不是WPF。
How can I set an individual margin on a view from XAML? 如何在XAML的视图上设置单个边距?
The linked Q&A indicates that the default implementation always sets all unspecified Margins to 0, even from code. 链接的Q&A表示默认实现始终将所有未指定的边距设置为0,即使是代码也是如此。
The solution is to create an AttachedProperty : 解决方案是创建一个AttachedProperty :
using System;
using Xamarin.Forms;
namespace Sample.Utils
{
/// <summary>
/// Allows setting Top,Left,Right,Bottom Margin independently from each other.
/// The default implementation does not allow that, even from code. This
/// attached property remembers the previously set margins and only modifies what you intend to modify.
/// </summary>
public static class Margin
{
public static readonly BindableProperty LeftProperty = BindableProperty.CreateAttached(
propertyName: "Left",
returnType: typeof(double),
declaringType: typeof(Margin),
propertyChanged: LeftPropertyChanged,
defaultValue: 0.0d);
private static void LeftPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
SetLeft(bindable, (double)newValue);
}
public static void SetLeft(BindableObject element, double value)
{
var view = element as View;
if (view != null)
{
Thickness currentMargin = (Xamarin.Forms.Thickness)view.GetValue(View.MarginProperty);
view.Margin = new Thickness(value, currentMargin.Top, currentMargin.Right, currentMargin.Bottom);
}
}
public static double GetLeft(BindableObject element)
{
View view = element as View;
if (view != null)
{
Thickness margin = (Xamarin.Forms.Thickness)view.GetValue(View.MarginProperty);
return margin.Left;
}
return (double)LeftProperty.DefaultValue;
}
}
}
Declare this in the same way for Bottom
, Top
, and Right
. 以相同的方式为Bottom
, Top
和Right
声明这一点。 Then use it in XAML like this: 然后在XAML中使用它,如下所示:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:utils="using:Sample.Utils"
x:Class="Sample.MyContentPage">
<StackLayout
Orientation="Vertical"
utils:Margin.Left="{StaticResource InnerPageContentMarginLeft}">
</StackLayout>
</ContentPage>
Sources: 资料来源:
* https://forums.xamarin.com/discussion/66026/use-attached-bindable-property-in-xaml * https://forums.xamarin.com/discussion/66026/use-attached-bindable-property-in-xaml
* https://stackoverflow.com/a/32408461/2550406 * https://stackoverflow.com/a/32408461/2550406
Unless I'm missing something, it's just a matter of specifying the Margin as follows: 除非我遗漏了什么,否则只需指定保证金如下:
<Label
Margin = "0,20,0,0"
Text = "Hello World!" />
The Margin is specified as left , top , right , bottom values. 保证金指定为左 , 上 , 右 , 下值。
Maybe try a valueConverter where you can send a value to converter through binding and return a Thickness object. 也许尝试一个valueConverter,你可以通过绑定向转换器发送一个值并返回一个Thickness对象。 Somewhat like 有点像
Margin ={{Binding Marginleft, Converter={StaticResource stringToThicknessConverter}}
where you pass a string and get back a thickness object like new thickness(0,marginleft,0,0)
. 你传递一个字符串并返回一个厚度对象,如new thickness(0,marginleft,0,0)
。
Also you could directly bind to a thickness type object in viewModel but that's a bad practice as it would create a View dependency in ViewModel which defeats the purpose of MVVM 你也可以直接绑定到viewModel中的厚度类型对象,但这是一个不好的做法,因为它会在ViewModel中创建一个View依赖,这违背了MVVM的目的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.