Default value at design time XAML

回眸只為那壹抹淺笑 提交于 2019-12-21 06:55:36

问题


I have a binded TextBlock, XAML:

<TextBlock Text="{Binding MyText}"/>

I know the FallbackValue can be used if the Binding isn't available, but this happens at run time ? Is there any way to show a default value at design time ? It would make things easier if I could see a value when designing my windows instead of an empty TextBlock.

Thanks


回答1:


If you would prefer a less verbose version of Ian Bamforth's answer, you can just do

<TextBlock Text="{Binding MyText, FallbackValue=None}"/>



回答2:


Adapting an example from this question.

This works for me - the text "None" is shown in the designer:

 <TextBlock>
    <TextBlock.Text>
        <Binding ElementName="root" Path="blah" FallbackValue="None" />
    </TextBlock.Text>
</TextBlock>

Hope that helps




回答3:


If you have this data bound and are using the MVVM architecture then setting a DEFAULT value for the model item it is bound to will display the value at design time

I am just using:

Model.cs:

private int frame = 999999;
public int Frame
{
  get { return frame; }
  set
  {
    frame = value;
    NotifyPropertyChanged(m => m.Frame);
  }
}

and in my XAML:

 <TextBlock Text="{Binding Path=Frame}"  />

and the default value of "999999" is being displayed in the designer



来源:https://stackoverflow.com/questions/17208665/default-value-at-design-time-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!