2017-01-19 3 views
0

Я следовал 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)); 
} 
+0

http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors – Mat

+1

"вход": Какой вклад? Я не против стены кода вообще (при условии, что это текст, а не скриншот), но вам нужно указать, где это больно. –

+2

Чтобы дать вам отличный ответ, это может помочь нам, если у вас есть взгляд на [спросить], если вы этого еще не сделали. Это может быть полезно, если вы можете предоставить [mcve]. – Mat

ответ

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("")); 
+0

Спасибо, я сделал это минуту назад и удалил часть ошибки из вопроса прямо перед вашим ответом :) Теперь речь идет только о хранении ввода в текстовом поле :) – eXPerience

Смежные вопросы