Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF错误:";无法向BarStaticItem“类型的对象添加内容;_Wpf_Xaml_Wpf Controls_Datatemplate_Contenttemplate - Fatal编程技术网

WPF错误:";无法向BarStaticItem“类型的对象添加内容;

WPF错误:";无法向BarStaticItem“类型的对象添加内容;,wpf,xaml,wpf-controls,datatemplate,contenttemplate,Wpf,Xaml,Wpf Controls,Datatemplate,Contenttemplate,我有这个XAML: <dxb:BarStaticItem> <TextBlock Text="{Binding MyStatusBarText}"></TextBlock> </dxb:BarStaticItem> 但是,我遇到了以下错误: 无法向BarStaticItem类型的对象添加内容 如何解决此问题,以便更改渲染项的颜色和样式?此XAML可以正常工作,但有其局限性(它不允许您根据要求设置显示文本的颜色): 首先,我们在窗口中定义

我有这个XAML:

<dxb:BarStaticItem>
  <TextBlock Text="{Binding MyStatusBarText}"></TextBlock>
</dxb:BarStaticItem> 

但是,我遇到了以下错误:

无法向BarStaticItem类型的对象添加内容


如何解决此问题,以便更改渲染项的颜色和样式?

此XAML可以正常工作,但有其局限性(它不允许您根据要求设置显示文本的颜色):

首先,我们在
窗口中定义
数据模板。这就是我们的
ContentTemplate
将指向的内容:

<Window.Resources>
    <DataTemplate x:Key="MyStatusBarContentTemplate">
        <!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
        <TextBlock Name="MyText"                
            Text="{Binding Path=MyStatusBarText, 
            RelativeSource={RelativeSource Mode=FindAncestor,
            AncestorType=Window}}">                
        </TextBlock>
    </DataTemplate>
</Window.Resources>
现在我们已经定义了一个自定义数据模板,我们可以做任何我们想做的事情。例如,我们可以添加一个
转换器
,如果状态栏包含文本
Error
(否则这是不可能的)

此答案还说明了如何使用
DataTemplate
显示大多数控件的自定义内容

更新

与其在窗口的资源中定义
DataTemplate
,不如将其定义为
BarStaticItem
的资源。这会将相关项保存在XAML中

这个特殊的XAML意味着,如果文本包含字符串
Error
,状态栏文本将自动变为红色,并且状态栏文本将自动以时间作为前缀。如果你想让我发布转换器的C代码,请告诉我

<dxb:BarStaticItem
  ContentTemplate="{DynamicResource MyStatusBarContentTemplate}">
  <dxb:BarStaticItem.Resources>
      <DataTemplate x:Key="MyStatusBarContentTemplate">
          <!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
          <TextBlock Name="MyText"
              Foreground="{Binding ElementName=MyText, Path=Text, Converter={StaticResource ColorRedIfTextContainsError}}"
              Text="{Binding Path=SettingsGlobalViewModel.StatusBarText, 
              RelativeSource={RelativeSource Mode=FindAncestor,
              AncestorType=Window},
              Converter={StaticResource PrefixStringWithTime}}">
          </TextBlock>
      </DataTemplate>
  </dxb:BarStaticItem.Resources>
</dxb:BarStaticItem> 

此XAML可以正常工作,但存在局限性(它不允许您根据要求设置显示文本的颜色):

首先,我们在
窗口中定义
数据模板。这就是我们的
ContentTemplate
将指向的内容:

<Window.Resources>
    <DataTemplate x:Key="MyStatusBarContentTemplate">
        <!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
        <TextBlock Name="MyText"                
            Text="{Binding Path=MyStatusBarText, 
            RelativeSource={RelativeSource Mode=FindAncestor,
            AncestorType=Window}}">                
        </TextBlock>
    </DataTemplate>
</Window.Resources>
现在我们已经定义了一个自定义数据模板,我们可以做任何我们想做的事情。例如,我们可以添加一个
转换器
,如果状态栏包含文本
Error
(否则这是不可能的)

此答案还说明了如何使用
DataTemplate
显示大多数控件的自定义内容

更新

与其在窗口的资源中定义
DataTemplate
,不如将其定义为
BarStaticItem
的资源。这会将相关项保存在XAML中

这个特殊的XAML意味着,如果文本包含字符串
Error
,状态栏文本将自动变为红色,并且状态栏文本将自动以时间作为前缀。如果你想让我发布转换器的C代码,请告诉我

<dxb:BarStaticItem
  ContentTemplate="{DynamicResource MyStatusBarContentTemplate}">
  <dxb:BarStaticItem.Resources>
      <DataTemplate x:Key="MyStatusBarContentTemplate">
          <!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
          <TextBlock Name="MyText"
              Foreground="{Binding ElementName=MyText, Path=Text, Converter={StaticResource ColorRedIfTextContainsError}}"
              Text="{Binding Path=SettingsGlobalViewModel.StatusBarText, 
              RelativeSource={RelativeSource Mode=FindAncestor,
              AncestorType=Window},
              Converter={StaticResource PrefixStringWithTime}}">
          </TextBlock>
      </DataTemplate>
  </dxb:BarStaticItem.Resources>
</dxb:BarStaticItem> 

假设
BarStaticItem
是一个
用户控件
..

我在一个部分cs文件中使用代码隐藏,其中(几乎)一切都完成了,取而代之的是
UIElement
observateCollection
(或任何您想要的元素)

1)创建一个名为
BarStaticItem.Children.cs
的相关分部类,然后添加所需的名称空间:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
2)在部分类声明上方添加一个
ContentProperty
标志,然后添加您的
子类
属性声明:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
6)差不多完成了,但必须在BarStaticItem.xaml文件中创建并命名一个UIElement,以包含添加的UIElements:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
你是谁不是我


假设
BarStaticItem
是一个
UserControl

我在一个部分cs文件中使用代码隐藏,其中(几乎)一切都完成了,取而代之的是
UIElement
observateCollection
(或任何您想要的元素)

1)创建一个名为
BarStaticItem.Children.cs
的相关分部类,然后添加所需的名称空间:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
2)在部分类声明上方添加一个
ContentProperty
标志,然后添加您的
子类
属性声明:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
6)差不多完成了,但必须在BarStaticItem.xaml文件中创建并命名一个UIElement,以包含添加的UIElements:

using System.Collections.ObjectModel; // ObservableCollection.
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler.
using System.Windows.Markup; // [ContentProperty()]
namespace YourNamespace.SubNamespace
{
    [ContentProperty("Children")] // <- here !
    public partial class BarStaticItem
    // (This is the related BarStaticItem.Children.cs file)
    {
        /// <summary>
        /// Gets the Children Property of this BarStaticItem.
        /// </summary>
        public ObservableCollection<UIElement> Children { get; private set; }
    }
}
namespace YourNamespace.SubNamespace
{
    public partial class BarStaticItem : UserControl
    // (This is the base BarStaticItem.xaml.cs file)
    {
        public BarStaticItem()
        {
            InitializeComponent();
            RegisterChildrenObservation(); // <- here !
        }
    }
}
<UserControl ...>
    <Grid 
        x:Name="MyChildContainer"><!-- HERE ! -->
    </Grid>
</UserControl>
你是谁不是我


神秘的选民是否愿意详细说明原因?这个解决方案对我来说非常有效(我花了一段时间才弄明白,这就是为什么我把它贴在上面,供我自己和其他人参考)。我只是仔细地重新阅读了你的答案。。在我写完我的之后。虽然我不是落选者,但你似乎没有在你的问题中很好地解释你的实际问题,这使得你自己的答案与被问的问题毫不接近(我假设,基于许多应该属于该问题的附加信息),神秘的落选者会愿意详细说明原因吗?这个解决方案对我来说非常有效(我花了一段时间才弄明白,这就是为什么我把它贴在上面,供我自己和其他人参考)。我只是仔细地重新阅读了你的答案。。在我写完我的之后。虽然我不是反对者,但你似乎没有在你的问题中很好地解释你的实际问题,这使得你自己的答案与被问的问题相差甚远(我假设,基于许多应该属于该问题的附加信息)