Extended.WPF.Toolkitを別ウィンドウに追加した際、Extended.WPF.Toolkitで編集できなくなりました。
###コードの説明
画像を表示させ、クリックが2回されたら、TextBoxを表示させて、Extended.WPF.Toolkitで編集できるようにする。
しかし、Extended.WPF.Toolkitで編集できないためご質問させていただきました。
発生している問題・エラーメッセージ
なし
該当のソースコード
MainWindow.xmal
xaml
1<Window x:Class="WpfApp4.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp4" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 10 11 <Window.Resources> 12 <ContextMenu x:Key="TextBoxContextMenu"> 13 <MenuItem Command="ApplicationCommands.Copy" /> 14 <MenuItem Command="ApplicationCommands.Cut" /> 15 <MenuItem Command="ApplicationCommands.Paste" /> 16 <MenuItem Click="DelMenuItem_Click" Header="削除" /> 17 </ContextMenu> 18 19 <Style x:Key="TextBoxStyle" TargetType="{x:Type local:TextBoxEx}"> 20 <Setter Property="AcceptsReturn" Value="True" /> 21 <Setter Property="AcceptsTab" Value="True" /> 22 <Setter Property="Background" Value="{x:Null}" /> 23 <Setter Property="BorderBrush" Value="{x:Null}" /> 24 <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" /> 25 <Setter Property="FontSize" Value="16px" /> 26 <Setter Property="FontWeight" Value="bold" /> 27 <Setter Property="Foreground" Value="Red" /> 28 <Setter Property="Text" Value="テキスト" /> 29 <Setter Property="TextWrapping" Value="Wrap" /> 30 <Setter Property="Template"> 31 <Setter.Value> 32 <ControlTemplate TargetType="{x:Type TextBox}"> 33 <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True"> 34 <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" /> 35 </Border> 36 <ControlTemplate.Triggers> 37 <Trigger Property="IsMouseOver" Value="true"> 38 <Setter TargetName="border" Property="BorderBrush" Value="#FF7EB4EA" /> 39 </Trigger> 40 <Trigger Property="IsFocused" Value="true"> 41 <Setter TargetName="border" Property="BorderBrush" Value="#FF7EB4EA" /> 42 </Trigger> 43 </ControlTemplate.Triggers> 44 </ControlTemplate> 45 </Setter.Value> 46 </Setter> 47 </Style> 48 </Window.Resources> 49 50 <Grid> 51 <Grid.ColumnDefinitions> 52 <ColumnDefinition/> 53 <ColumnDefinition/> 54 55 </Grid.ColumnDefinitions> 56 57 58 <StackPanel Orientation="Vertical"> 59 <Button x:Name="button" Content="押してください" Click="button_Click"/> 60 <Button x:Name="button_open" Content="開く" Click="button_open_Click"/> 61 </StackPanel> 62 63 64 65 <Canvas Grid.Column="1" x:Name="canvas" MouseLeftButtonDown="Canvas_MouseLeftButtonDown"> 66 <Image Source="Resources/noimage.png" Grid.Column="1" Height="321" Width="222" Canvas.Top="51" x:Name="imageBox" Canvas.Left="93"/> 67 </Canvas> 68 69 70 71 72 73 </Grid> 74 75 76 77 78</Window> 79
MainWindow.xaml.cs
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15using Microsoft.Win32; 16using System.IO; 17using System.ComponentModel; 18 19namespace WpfApp4 20{ 21 /// <summary> 22 /// MainWindow.xaml の相互作用ロジック 23 /// </summary> 24 public partial class MainWindow : Window 25 { 26 public string FilePath = null; 27 public MainWindow() => InitializeComponent(); 28 29 30 31 32 33 34 35 36 //objectを設定 37 public void TextBox_GotFocus(object sender, RoutedEventArgs e) 38 { 39 AdditionalWindow additionalWindow = new AdditionalWindow(); 40 additionalWindow.propertyGrid.SelectedObject = sender; 41 } 42 43 44 45 46 47 48 49 //ファイルを開く 50 private void button_open_Click(object sender, RoutedEventArgs e) 51 { 52 OpenFileDialog openFileDialog = new OpenFileDialog(); 53 if (openFileDialog.ShowDialog() == true) 54 { 55 FilePath = openFileDialog.FileName; 56 } 57 BitmapImage bitmapImage = new BitmapImage(new Uri(FilePath)); 58 59 imageBox.Source = bitmapImage; 60 } 61 62 63 64 65 66 67 private void button_Click(object sender, RoutedEventArgs e) 68 { 69 AdditionalWindow additionalWindow = new AdditionalWindow(); 70 additionalWindow.Show(); 71 additionalWindow.Owner = this; 72 73 74 75 76 77 78 } 79 80 81 private void DelMenuItem_Click(object sender, RoutedEventArgs e) 82 { 83 AdditionalWindow additionalWindow = new AdditionalWindow(); 84 MainWindow mainWindow = new MainWindow(); 85 86 if (sender is MenuItem menuItem) 87 { 88 if (menuItem.Parent is ContextMenu contextMenu) 89 { 90 if (contextMenu.PlacementTarget is TextBoxEx textBox) 91 { 92 additionalWindow.propertyGrid.SelectedObject = null; 93 textBox.GotFocus -= TextBox_GotFocus; 94 mainWindow.canvas.Children.Remove(textBox); 95 } 96 } 97 } 98 99 } 100 101 102 103 private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 104 { 105 if (e.ClickCount == 2) 106 { 107 var textBox = new TextBoxEx 108 { 109 Style = FindResource("TextBoxStyle") as Style, 110 CanvasLeft = e.GetPosition(canvas).X, 111 CanvasTop = e.GetPosition(canvas).Y 112 }; 113 textBox.GotFocus += TextBox_GotFocus; 114 canvas.Children.Add(textBox); 115 } 116 } 117 } 118} 119
AdditionalWindow.xaml
xaml
1<Window x:Class="WpfApp4.AdditionalWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp4" 7 mc:Ignorable="d" 8 xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 9 Title="AdditionalWindow" Height="300" Width="300"> 10 11 12 13 14 <Grid> 15 16 17 <xctk:PropertyGrid x:Name="propertyGrid"/> 18 19 20 </Grid> 21</Window> 22
AdditionalWindow.xaml.cs
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Shapes; 14using System.IO; 15using Microsoft.Win32; 16 17namespace WpfApp4 18{ 19 20 21 public class TextBoxEx : TextBox 22 { 23 public static readonly DependencyProperty CanvasLeftProperty 24 = DependencyProperty.Register(nameof(CanvasLeft), typeof(double), typeof(TextBoxEx), 25 new FrameworkPropertyMetadata(0d, new PropertyChangedCallback(OnCanvasLeftChanged))); 26 public double CanvasLeft { get => (double)GetValue(CanvasLeftProperty); set => SetValue(CanvasLeftProperty, value); } 27 private static void OnCanvasLeftChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 28 { 29 if (obj is TextBoxEx ctrl) Canvas.SetLeft(ctrl, ctrl.CanvasLeft); 30 } 31 32 //Canvas.Topをラップ 33 public static readonly DependencyProperty CanvasTopProperty 34 = DependencyProperty.Register(nameof(CanvasTop), typeof(double), typeof(TextBoxEx), 35 new FrameworkPropertyMetadata(0d, new PropertyChangedCallback(OnCanvasTopChanged))); 36 public double CanvasTop { get => (double)GetValue(CanvasTopProperty); set => SetValue(CanvasTopProperty, value); } 37 private static void OnCanvasTopChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 38 { 39 if (obj is TextBoxEx ctrl) Canvas.SetTop(ctrl, ctrl.CanvasTop); 40 } 41 } 42 43 /// <summary> 44 /// AdditionalWindow.xaml の相互作用ロジック 45 /// </summary> 46 public partial class AdditionalWindow : Window 47 { 48 public AdditionalWindow() => InitializeComponent(); 49 50 51 52 53 54 55 56 57 } 58} 59
どうかご教授よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/29 12:09
退会済みユーザー
2020/01/29 12:09
2020/01/29 12:30
退会済みユーザー
2020/01/30 01:11