Default value at design time XAML

2019-04-03 13:43发布

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

3条回答
地球回转人心会变
2楼-- · 2019-04-03 13:55

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

<TextBlock Text="{Binding MyText, FallbackValue=None}"/>
查看更多
叛逆
3楼-- · 2019-04-03 14:00

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

查看更多
我命由我不由天
4楼-- · 2019-04-03 14:01

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

查看更多
登录 后发表回答