Conditional Resources Creation WPF XAML Design / Run time

前端 未结 2 1522
长发绾君心
长发绾君心 2021-01-24 02:06

Following a first question on WPF Cascading Binding,

I remarked that I had more Resources than desired defined in both the MainWindow

2条回答
  •  礼貌的吻别
    2021-01-24 02:47

    I prepared simplified way out displaying one data during design time and latter during run time. Hopefully you will find it useful and adjust to your case.

    XAML:

    
        
            
        
        
            
        
        
    
    
        
            
                
                
            
        
    
    

    Converter:

     class IsInDesignModeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
                return values[0];
            return values[1];
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    As a result it display Design mode text during design time and Run time when running. In your case instead of TextBlock you can insert already defined resources.

提交回复
热议问题