我有一個適當的工作,沒有綁定,而其字體大小應動態更改的文本框控件。如何爲wpf文本框綁定fontsize?
我想做的正確,並使用綁定工作。
我試圖綁定(XAML)的字號使用:
<UserControl x:Class="<ClassName>"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Focusable="True"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="_textBox" Visibility="Visible" xml:space="preserve"
Background="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Background}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Foreground}"
FontFamily="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=FontFamily}"
BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=BorderBrush}"
Text="{Binding MultilineText}"
FontSize="{Binding Path=MultilineFontSize}"
KeyUp="_textBox_KeyUp"
PreviewTextInput="_textBox_PreviewTextInput"
DataObject.Pasting="_textBox_Pasting"
VerticalContentAlignment="Top"
PreviewKeyDown="TextBox_OnPreviewKeyDown"
TextWrapping="Wrap"
ScrollViewer.VerticalScrollBarVisibility="Visible"
/>
</Grid>
和在後面的代碼:
private double _multilineFontSize;
public double MultilineFontSize
{
get { return GetBestFittingFontSize(); }
set
{
if (value != _multilineFontSize)
{
_multilineFontSize = value;
OnPropertyChanged("MultilineFontSize");
}
}
}
爲_multilineFontSize的唯一用途是取代_textbox.Text無論我在哪裏使用(在事件等)。
GetBestFittingFontSize()是一個函數(適用),並計算我需要使用的字體大小。按照給定的。它返回一倍。
它不起作用。有沒有人知道爲什麼? (可能是一些DataContext問題?)
我們不能說因爲你沒有給我們的XAML(我們甚至不知道控制型),我們不知道是什麼'返回GetBestFittingFontSize();'是,如何'_multilineFontSize '設置好,目前的結果是什麼等等。 –
@PatrickHofman - 感謝您的反饋。增加了很多細節(我想把它縮短......這可能太短了......對不起!) – MTZ
我投票重新開放。希望別人會跟隨。 –