DesignTime data not showing in Blend when bound against CollectionViewSource

后端 未结 3 1057
名媛妹妹
名媛妹妹 2021-02-07 09:55

I have a datatemplate for a viewmodel where an itemscontrol is bound against a CollectionViewSource (to enable sorting in xaml).



        
3条回答
  •  面向向阳花
    2021-02-07 11:02

    1. I am not sure x:Static is supposed to work in d:DataContext, I think only d:DesignInstance or d:DesignData could.
    2. Did you test the design time data and sure that indeed filled with data?
    3. Try specifying the d:IsDesignTimeCreatable=True property in the d:DesignInstance.
    4. Although this is Silverlight specific I am sure it might give you some hint.

    It should generally look like this:

    d:DataContext="{d:DesignInstance Type=vm:EquipmentViewModel, IsDesignTimeCreatable=True}"

    You could use the same ViewModel for both runtime and designtime, make a IsInDesignTime property in you ViewModelBase and return data appropriately.
    Example:

    private static bool? _isInDesignMode;
    public static bool IsInDesignModeStatic
    {
        get
        {
            if (!_isInDesignMode.HasValue)
            {
                var prop = DesignerProperties.IsInDesignModeProperty;
                _isInDesignMode
                    = (bool)DependencyPropertyDescriptor
                    .FromProperty(prop, typeof(FrameworkElement))
                    .Metadata.DefaultValue;
            }
    
            return _isInDesignMode.Value;
        }
    }
    

    Note: I would encourage you to use StaticResources (rather than DynamicResources) for templates or styles that are not meant to change at runtime. Read this for more info.

提交回复
热议问题