Hello,
Welcome to Microsoft Q&A!
If you could use a UserControl
as the container of the TextBox
, then you could create some properties which represents TextBox.FontSize and TextBox.FontFamily . Then you could implement INotifyPropertyChanged
in the UserControl. So when the custom properties are changed, you will get notified and change the Height of the TextBox.
I've made a sample to test. I placed a TextBox
in XAML as the source. Then pass it to the UserControl
. The target TextBox
binds to properties of the UserControl
. After that, set the UserControl
as the datacontext of the target TextBox
UserControl.CS
public sealed partial class MyUserControl1 : UserControl, INotifyPropertyChanged
{
private TextBox SourceTextBox;
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChangedEvent(string name)
{
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
if (name.Equals("MyfontSize")||name.Equals("MyfontFamily"))
{
MyTextBoxHeight = MyTextBoxHeight + 50;
}
}
public MyUserControl1(TextBox textBox)
{
this.InitializeComponent();
SourceTextBox = textBox;
}
// fontSize property
public double MyTextBoxHeight
{
get
{
return SourceTextBox.Height;
}
set
{
SourceTextBox.Height = value;
RaisePropertyChangedEvent("MyTextBoxHeight");
}
}
// fontSize property
public double MyfontSize
{
get
{
return SourceTextBox.FontSize;
}
set
{
SourceTextBox.FontSize = value;
RaisePropertyChangedEvent("MyfontSize");
}
}
// fontfamily property
public FontFamily MyfontFamily
{
get
{
return SourceTextBox.FontFamily;
}
set
{
SourceTextBox.FontFamily = value;
RaisePropertyChangedEvent("MyfontFamily");
}
}
}
MainPage.CS
public sealed partial class MainPage : Page
{
public MyUserControl1 sourceControl { get; set; }
public MainPage()
{
this.InitializeComponent();
sourceControl = new MyUserControl1(SourceBox);
TargetBox.DataContext = sourceControl;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
sourceControl.MyfontSize = 50;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
sourceControl.MyfontFamily = new FontFamily("Times New Roman");
}
}
MainPage.Xaml
<StackPanel>
<TextBox x:Name="SourceBox" FontSize="30" FontFamily="Arial" Height="100" Width="400"/>
<TextBox x:Name="TargetBox" Width="400" FontSize="{Binding MyfontSize,Mode=TwoWay}" FontFamily="{Binding MyfontFamily,Mode=TwoWay}" Height="{Binding MyTextBoxHeight,Mode=TwoWay}"/>
<Button Content="ChangeSize" Click="Button_Click" />
<Button Content="ChangeFont" Click="Button_Click_1" />
</StackPanel>
</Page>
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.