8

バインドされたコンテンツはempty、UIデザインモードで文字列として表示されます。それらのコンテンツに偽の値を表示したいのですが、方法がわかりません。

方法を知っているなら共有してください。ありがとうございました!

4

4 に答える 4

12

Visual Studio 2010 でデザイン時データを取得する簡単な方法は、デザイン データ コンテキストを使用することです。Window と ViewModel の短い例。DataContext の場合、d:DataContext はデザイン モードで使用され、StaticResource は実行時に使用されます。設計に別の ViewModel を使用することもできますが、この例では両方に同じ ViewModel を使用します。

<Window ...
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:DesignTimeData"
        mc:Ignorable="d"            
        d:DataContext="{d:DesignInstance local:MyViewModel,
                        IsDesignTimeCreatable=True}">
    <Window.Resources>
        <local:MyViewModel x:Key="MyViewModel" />
    </Window.Resources>
    <Window.DataContext>
        <StaticResource ResourceKey="MyViewModel"/>
    </Window.DataContext>
    <StackPanel>
        <TextBox Text="{Binding MyText}"
                 Width="75"
                 Height="25"
                 Margin="6"/>
    </StackPanel>
</Window>

また、ViewModels プロパティ MyText で、デザイン モードかどうかを確認し、その場合は別のものを返します。

public class MyViewModel : INotifyPropertyChanged
{
    public MyViewModel()
    {
        MyText = "Runtime-Text";
    }

    private string m_myText;
    public string MyText
    {
        get
        {
            // Or you can use
            // DesignerProperties.GetIsInDesignMode(this)
            if (Designer.IsDesignMode)
            {
                return "Design-Text";
            }
            return m_myText;
        }
        set
        {
            m_myText = value;
            OnPropertyChanged("MyText");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

ここにある Designer.cs は、次のようになります。

public static class Designer
{
    private static readonly bool isDesignMode;
    public static bool IsDesignMode
    {
        get { return isDesignMode; }
    }
    static Designer()
    {
        DependencyProperty prop =
            DesignerProperties.IsInDesignModeProperty;
        isDesignMode =
            (bool)DependencyPropertyDescriptor.
                FromProperty(prop, typeof(FrameworkElement))
                      .Metadata.DefaultValue;
    }
}
于 2010-11-30T17:12:10.330 に答える
2

DesignMode プロパティを使用して、設計時かどうかを確認できます ( http://msdn.microsoft.com/en-us/library/c58hb4bw(vs.71).aspx )

この質問でそれを行う方法についてはさらに考えがありますが、実際の結論はありません。

于 2010-11-30T15:40:00.693 に答える
-4

コンテンツを別のプロパティでラップし、値が空かどうかをテストできます。その場合、必要な偽の値を返します。

private string _content;
public string Content
{
    get
    { 
        if (_content != "") return _content;
        else return "FAKE";
    }
    set { _content= value; }
}
于 2010-11-30T13:21:36.913 に答える