C# 条件资源创建WPF XAML设计/运行时
在第一个问题之后 我注意到我在主窗口和用户控件中定义的资源比预期的要多: 这在Snoop中很常见C# 条件资源创建WPF XAML设计/运行时,c#,wpf,binding,datacontext,resourcedictionary,C#,Wpf,Binding,Datacontext,Resourcedictionary,在第一个问题之后 我注意到我在主窗口和用户控件中定义的资源比预期的要多: 这在Snoop中很常见 public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); if ( DesignerProperties.GetIsInDesignMode(this)) { //Do w
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
if ( DesignerProperties.GetIsInDesignMode(this))
{
//Do what you want here for design time specific actions
}
}
}
- MainWindow XAML有一个MainWindow ViewModel的“实例”,它将2用户控件绘制为绿色和蓝色
- 另外,两个usercontrol中的每一个都有两个usercontrol viewmodels实例,它们会将内圈涂成红色
NGI我准备了一种简化的方法,在设计时显示一个数据,在运行时显示后一个数据。希望你会发现它有用,并适应你的情况
XAML:
<Window.Resources>
<DataTemplate x:Key="DesignModeDataTemplate">
<TextBlock Text="Design mode"/>
</DataTemplate>
<DataTemplate x:Key="RunTimeDataTemplate">
<TextBlock Text="Run Time"/>
</DataTemplate>
<local:IsInDesignModeConverter x:Key="IsInDesignModeConverter"/>
</Window.Resources>
<ContentControl>
<ContentControl.ContentTemplate>
<MultiBinding Converter="{StaticResource IsInDesignModeConverter}">
<Binding Source="{StaticResource DesignModeDataTemplate}"/>
<Binding Source="{StaticResource RunTimeDataTemplate}"/>
</MultiBinding>
</ContentControl.ContentTemplate>
</ContentControl>
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();
}
}
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
if ( DesignerProperties.GetIsInDesignMode(this))
{
//Do what you want here for design time specific actions
}
}
}
因此,它在设计时和运行时显示设计模式文本。
在您的情况下,您可以插入已定义的资源而不是TextBlock。我将最终尝试回答我自己的问题 结果是等待在较低级别的ViewModel实例中没有“重复资源” snoop中的结果: XAML代码
<UserControl x:Class="BindingMCVE.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BindingMCVE"
d:DataContext="{d:DesignInstance {x:Type vm_nmspc:my_usercontrol_vm}, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100"
xmlns:vm_nmspc="clr-namespace:BindingMCVE.ViewModel"
>
<UserControl.Resources>
<!--No resources created in XAML-->
</UserControl.Resources>
<Grid>
<StackPanel Height="100" Width="100">
<Ellipse Fill="{Binding my_color}" Height="50" Width="50" />
</StackPanel>
</Grid>
一项在设计期间,后一项在运行期间?@Maximus:UserControl资源仅在设计此UserControl的外观时需要(圆圈为红色)。在主窗口设计期间或在应用程序运行期间,主窗口资源将强制使用其数据(两个圆圈分别为绿色和蓝色),从而浪费UserControl资源@Maximus:Hi,我发现,在运行整个应用程序时,在Subcrol中浪费viewmodel资源的问题更容易解决。告诉我你对这个解决方案的看法。@Maximus:非常感谢你的回答。我只是编译了一下,看到了预期的结果。让我花些时间彻底理解它,并使它适应我的情况