从附加文本框

时间:2017-01-19 14:29:04

标签: c# wpf xaml

我跟着this tutorial,现在面临在设计器项目中添加文本的问题。这就是程序的样子。 enter image description here

在左侧,您可以看到图表,它的布局在FlowChartStencil.xaml中指定。在右边有一个Canvas,其中包含DesignerItem类的元素。出现带有文本的文本框,但我无法将文本输入保存到DesignerItem类的对象。 DesignerItem.xaml如下

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:s="clr-namespace:WPFCanvas"
                xmlns:c="clr-namespace:WPFCanvas.Controls">

<ContextMenu x:Key="DesignerItemContextMenu">
    ...
</ContextMenu>

<!-- Connector Style -->
<Style TargetType="{x:Type s:Connector}">
    ...
</Style>

<!-- ConnectorDecoratorTemplate Default Template -->
<ControlTemplate x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}">
    ...
</ControlTemplate>

<!-- ResizeDecorator Default Template -->
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
    ...
</ControlTemplate>

<!-- DragThumb Default Template -->
<Style TargetType="{x:Type c:DragThumb}">
    ...
</Style>

<!-- TextBoxDecorator Default Template -->
<ControlTemplate x:Key="TextBoxDecoratorTemplate" TargetType="{x:Type Control}">
    <ContentControl Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBox FontSize="11" Margin="1,1,0,0" TextWrapping="Wrap" AcceptsReturn="True"
                 Background="Transparent" Text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit."/>
    </ContentControl>
</ControlTemplate>

<!-- DesignerItem Style -->
<Style TargetType="{x:Type s:DesignerItem}">
    <Setter Property="MinWidth" Value="25"/>
    <Setter Property="MinHeight" Value="25"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type s:DesignerItem}">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
            ContextMenu="{StaticResource DesignerItemContextMenu}">
                    <!-- DragThumb -->
                    <c:DragThumb x:Name="DragThumb" Cursor="SizeAll"/>
                    <!-- ResizeDecorator -->
                    <Control x:Name="ResizeDecorator"
                 Visibility="Collapsed"
                 Template="{StaticResource ResizeDecoratorTemplate}"/>

                    <!-- ContentPresenter -->
                    <ContentPresenter x:Name="ContentPresenter"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          Content="{TemplateBinding ContentControl.Content}"
                          Margin="{TemplateBinding ContentControl.Padding}"/>
                    <!-- ConnectorDecorator -->
                    <Control x:Name="ConnectorDecorator"
                 Visibility="Hidden"
                 Template="{StaticResource ConnectorDecoratorTemplate}"/>

                    <!-- TextBoxDecorator -->
                    <Control x:Name="TextBoxDecorator"
                 Template="{StaticResource TextBoxDecoratorTemplate}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}">
                        <Setter TargetName="TextBoxDecorator" Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                    <Trigger Property="Text" Value="true">
                        <Setter TargetName="TextBoxDecorator" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}">
                        <Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="ConnectorDecorator" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
                        <Setter TargetName="ConnectorDecorator" Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

DesignerItem.cs(没有不必要的代码):

//These attributes identify the types of the named parts that are used for templating
[TemplatePart(Name = "DragThumb", Type = typeof(DragThumb))]
[TemplatePart(Name = "ResizeDecorator", Type = typeof(Control))]
[TemplatePart(Name = "ConnectorDecorator", Type = typeof(Control))]
[TemplatePart(Name = "ContentPresenter", Type = typeof(ContentPresenter))]
public class DesignerItem : ContentControl, ISelectable, IGroupable
{        
    public bool IsSelected
    {
        get { return (bool)GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, value); }
    }
    public static readonly DependencyProperty IsSelectedProperty =
       DependencyProperty.Register("IsSelected", typeof(bool),
                                    typeof(DesignerItem),
                                    new FrameworkPropertyMetadata(false));        

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
       DependencyProperty.Register("Text", typeof(string),
                                    typeof(DesignerItem));
}

1 个答案:

答案 0 :(得分:2)

更改此内容:

public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
       DependencyProperty.Register("Text", typeof(string),
                                    typeof(DesignerItem),
                                    new FrameworkPropertyMetadata(false)); // thats the mistake you cant have boolean here

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
   DependencyProperty.Register("Text", typeof(string),
                                typeof(DesignerItem),
                                new FrameworkPropertyMetadata(""));