Edw*_*uay 44 c# wpf xaml margins
我可以在代码中单独设置边距,但是如何在XAML中设置边距,例如我该怎么做:
伪代码:
<StackPanel Margin.Top="{Binding TopMargin}">
Run Code Online (Sandbox Code Playgroud)
Car*_*rlo 52
这不是你想要的吗?
<StackPanel Margin="0,10,0,0" />
Run Code Online (Sandbox Code Playgroud)
第一个值是左边距,然后是顶部,然后是右边,最后一个但不是最小值.
我不确定你是否想将它绑定到某个东西,但如果没有,那就行了.
Ken*_*art 37
关键是要意识到在代码中设置它是这样的:
sp2.Margin = new System.Windows.Thickness{ Left = 5 };
Run Code Online (Sandbox Code Playgroud)
相当于:
sp2.Margin = new System.Windows.Thickness{ Left = 5, Top = 0, Right = 0, Bottom = 0 };
Run Code Online (Sandbox Code Playgroud)
您不能通过代码或XAML在Thickness实例中设置单个值.如果您没有设置某些值,它们将隐式为零.因此,您可以这样做将您在其他问题中接受的代码示例转换为XAML等效项:
<StackPanel Margin="{Binding TopMargin, Converter={StaticResource MyConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
其中,MyConverter刚刚返回Thickness的是只设置Top并留下所有其他值为零.
当然,你可以写你自己的控件,不公开为依赖属性,这些单独的值,使你的代码干净了一点:
<CustomBorder TopMargin="{Binding TopMargin}">
</CustomBorder>
Run Code Online (Sandbox Code Playgroud)
G.Y*_*G.Y 22
这属于WPF修正案:
您无法使用附加属性或边距的绑定动态设置元素的位置,而无需在后面写几行代码.
不要将此技术与其他技术进行比较.
您的问题列在#9.
刚刚编写了一些附加属性,可以轻松地从绑定或静态资源设置单独的 Margin 值:
public class Margin
{
public static readonly DependencyProperty LeftProperty = DependencyProperty.RegisterAttached(
"Left",
typeof(double),
typeof(Margin),
new PropertyMetadata(0.0));
public static void SetLeft(UIElement element, double value)
{
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
Thickness currentMargin = frameworkElement.Margin;
frameworkElement.Margin = new Thickness(value, currentMargin.Top, currentMargin.Right, currentMargin.Bottom);
}
}
public static double GetLeft(UIElement element)
{
return 0;
}
public static readonly DependencyProperty TopProperty = DependencyProperty.RegisterAttached(
"Top",
typeof(double),
typeof(Margin),
new PropertyMetadata(0.0));
public static void SetTop(UIElement element, double value)
{
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
Thickness currentMargin = frameworkElement.Margin;
frameworkElement.Margin = new Thickness(currentMargin.Left, value, currentMargin.Right, currentMargin.Bottom);
}
}
public static double GetTop(UIElement element)
{
return 0;
}
public static readonly DependencyProperty RightProperty = DependencyProperty.RegisterAttached(
"Right",
typeof(double),
typeof(Margin),
new PropertyMetadata(0.0));
public static void SetRight(UIElement element, double value)
{
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
Thickness currentMargin = frameworkElement.Margin;
frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, value, currentMargin.Bottom);
}
}
public static double GetRight(UIElement element)
{
return 0;
}
public static readonly DependencyProperty BottomProperty = DependencyProperty.RegisterAttached(
"Bottom",
typeof(double),
typeof(Margin),
new PropertyMetadata(0.0));
public static void SetBottom(UIElement element, double value)
{
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
Thickness currentMargin = frameworkElement.Margin;
frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, value);
}
}
public static double GetBottom(UIElement element)
{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<TextBlock Text="Test"
app:Margin.Top="{Binding MyValue}"
app:Margin.Right="{StaticResource MyResource}"
app:Margin.Bottom="20" />
Run Code Online (Sandbox Code Playgroud)
在 UWP 中进行了测试,但这应该适用于任何基于 XAML 的框架。好处是它们不会覆盖边距上的其他值,因此您也可以将它们组合起来。
| 归档时间: |
|
| 查看次数: |
61316 次 |
| 最近记录: |