wpf 如何为 UIElement.Margin 上的绑定设置 FallbackValue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13346352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:09:22  来源:igfitidea点击:

How do I set the FallbackValue for a Binding on UIElement.Margin?

wpfxamlbindingsyntax

提问by RoelF

Consider the following bit of code:

考虑以下代码:

<UserControl x:Name=root>
....
    <TextBlock Text="Hello World" Margin="{Binding ElementName=root, Path=LeftButtonMargin}"/>
....
</UserControl>

Now, what is the syntax for setting the FallBackvalueon the Binding?
I've tried some different options already, but I cannot seem to find the correct syntax:

现在,在 Binding 上设置 的语法是什么FallBackvalue
我已经尝试了一些不同的选项,但似乎找不到正确的语法:

Margin="{Binding ElementName=root, Path=LeftButtonMargin, FallBackValue={}10,10,0,0}"
Margin="{Binding ElementName=root, Path=LeftButtonMargin, FallBackValue={}{10,10,0,0}}"
Margin="{Binding ElementName=root, Path=LeftButtonMargin, FallBackValue={}"10,10,0,0"}"

Or is this not possible at all? Basically, I need these values at design time...

或者这根本不可能?基本上,我在设计时需要这些值......

采纳答案by Davut Gürbüz

Hopefully works,

希望有效,

<UserControl.Resources>
 <Thickness x:Key="MyMargin" Bottom="5" Top="10">

 </Thickness>
<UserControl.Resources>


<TextBlock Text="Hello World" 
Margin="{Binding ...,FallBackValue={StaticResource MyMargin}}"/>

回答by Sergey

More simple: use single quotes

更简单:使用单引号

Margin="{Binding ElementName=root, Path=LeftButtonMargin, FallBackValue='10,10,0,0'}"

回答by stukselbax

This is works for me:

这对我有用:

    <TextBlock Text="Hello World">
        <TextBlock.Margin>
            <Binding ElementName="root" Path="LeftButtonMargin" FallbackValue="10, 10, 0, 0" />
        </TextBlock.Margin>
    </TextBlock>

but its rather big...

但它相当大...