トップ回答者
WidthやHeight プロパティをユーザーコントロール側で取得したい

質問
-
VS2010初心者です。
環境は、Win7 32bit VS2010 C# .NetFramework 4.0
WPFにて開発中です。よろしくお願いします。XAML側で、設定したWidthやHeight プロパティを、コントロール側でうけとって、
コントロールに配置されているテキストボックスにセットしたいのですが、
プロパティをひろうことができません。どのようにして取得できるのでしょうか?
メインプログラム画面 XAML から <z:DecompoTextBox > でユーザーコンロールを配置
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:z="clr-namespace:WpfReportsTool;assembly=WpfReportsTool" xmlns:e="clr-namespace:WpfApplication1" xmlns:System="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown" KeyUp="Window_KeyUp"> <Window.Resources> <!-- フォーカスがある TextBox の背景色を変更するスタイル --> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="SkyBlue"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Canvas Name="CanvasNN" Height="310" Width="502"> <TextBox Canvas.Left="97" Canvas.Top="158" Height="26" Name="textBox5" Width="18" /> <TextBox Canvas.Left="121" Canvas.Top="158" Height="26" Name="textBox6" Width="18" /> <TextBox Canvas.Left="145" Canvas.Top="158" Height="26" Name="textBox7" Width="18" /> <z:DecompoTextBox Width="66" Canvas.Left="97" Canvas.Top="190" Name="decompoTextBox1"
JustLOrR="Right" /> </Canvas> </Window>z:DecompoTextBox にセットしている Width を ユーザーコントロール側で取得したいと考えています。
ユーザーコントロール DecompoTextBox
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace WpfReportsTool { /// <summary> /// DecompoTextBox.xaml の相互作用ロジック /// </summary> public partial class DecompoTextBox : UserControl { public List<UIElement> UIEList = new List<UIElement>(); public DecompoTextBox() { InitializeComponent(); } //プロパティ #region public Justification JustLOrR { get; set; } public double Width { get; set; } //<---これでは継承しているのでエラー #endregion } }
別のプロパティを作って取得する方法になるのでしょうか?
- 編集済み Michi.K 2011年11月21日 9:27 コード修正
回答
-
XAMLでBindingつかって取るのも可能ですし、SizeChangedイベントで取るのも可能です。
<UserControl x:Class="WpfApplication1.DecompoTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="uc"> <StackPanel Background="LightGreen"> <TextBox x:Name="txb1" /> <TextBox x:Name="txb2" Width="{Binding Path=ActualWidth,ElementName=uc,Mode=OneWay}" Text="{Binding Path=Height,Mode=Oneway,ElementName=uc}" /> </StackPanel> </UserControl>
public DecompoTextBox() { InitializeComponent(); this.SizeChanged += new SizeChangedEventHandler(DecompoTextBox_SizeChanged); } void DecompoTextBox_SizeChanged(object sender, SizeChangedEventArgs e) { txb1.Width = e.NewSize.Width / 2; txb1.Height = e.NewSize.Height / 2; }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)- 回答としてマーク Michi.K 2011年11月21日 22:16
すべての返信
-
XAMLでBindingつかって取るのも可能ですし、SizeChangedイベントで取るのも可能です。
<UserControl x:Class="WpfApplication1.DecompoTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="uc"> <StackPanel Background="LightGreen"> <TextBox x:Name="txb1" /> <TextBox x:Name="txb2" Width="{Binding Path=ActualWidth,ElementName=uc,Mode=OneWay}" Text="{Binding Path=Height,Mode=Oneway,ElementName=uc}" /> </StackPanel> </UserControl>
public DecompoTextBox() { InitializeComponent(); this.SizeChanged += new SizeChangedEventHandler(DecompoTextBox_SizeChanged); } void DecompoTextBox_SizeChanged(object sender, SizeChangedEventArgs e) { txb1.Width = e.NewSize.Width / 2; txb1.Height = e.NewSize.Height / 2; }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)- 回答としてマーク Michi.K 2011年11月21日 22:16