开发者

WPF User Control extending border class. "Does not support direct content"?

开发者 https://www.devze.com 2023-02-17 19:35 出处:网络
I am producing graphics for a process control system and I would like to create a system border which would visually wrap the various sub system being displayed in the process mimic. I could use a reg

I am producing graphics for a process control system and I would like to create a system border which would visually wrap the various sub system being displayed in the process mimic. I could use a regular border for this except I want it to not only changing color reflecting system status, but also popping up small "balloons" indicating the piece of the system that is in alarm state.

WPF User Control extending border class. "Does not support direct content"?

I created a test project with a User Control and added a ListBox (for the balloons) and a ContentPresenter element wrapped in a border control. However, whenever I use this new control in another app, it wont allow me to add content to it. I've tried messing some with the ContentPropertyAttribute and properties of the ContentPresenter element, but I feel I am in the blind here.

<UserControl x:Class="SystemStatusBorder.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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Canvas Height="290" Width="303">        
        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <ContentPresenter/>
        </Border>
        <ListBox Canvas.Right="0" Canvas.Bottom="0">
            <ListBox.RenderTransform>
                <TranslateTransform X="20"></TranslateTransform开发者_JAVA技巧>
            </ListBox.RenderTransform>
            <ListBoxItem>TagA</ListBoxItem>
            <ListBoxItem>TagB</ListBoxItem>
        </ListBox>
    </Canvas>
</UserControl>

I don't get it. What more should it need other than just the existence of a contentpresenter? UserControl subclasses ContentControl so I would have thought the wiring was in place. Eventually, I want it to be used something like this:

<SystemBorder>
  <SystemBorder.MonitoredTags>
        <List of relevant tags for the specific sub system goes here>
  </SystemBorder.MonitoredTags>
  <regular content goes here>  
</SystemBorder>


To create your own container control, you must create a new custom control (not a UserControl).

Make your new control inherit from ContentControl.

Custom Controls don't have their own XAML. Instead, they are assigned a ControlTemplate.

When you create your first Custom Control, the IDE will create a new file Themes\Generic.xaml.

This is where the template for your control is. You can modify this template to match the XAML in your question. This will support the ContentPresenter element.

I found a very good walkthrough here.

0

精彩评论

暂无评论...
验证码 换一张
取 消