【问题标题】:Cannot Set Foreground For TextBox in DataTemplate in WPF无法在 WPF 中的 DataTemplate 中为文本框设置前景
【发布时间】:2015-02-09 16:48:17
【问题描述】:

我正在尝试为DataTemplate 中指定的TextBox 设置Foreground 属性,但调用不起作用。

我有一个带有以下 XAML 的 UserControl

<UserControl x:Class="TextBoxColourTest.TextFrameControl"
             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"
             xmlns:clrtest="clr-namespace:TextBoxColourTest"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <DataTemplate x:Key="EditModeTemplate">
            <TextBox Text="Hello"></TextBox>
        </DataTemplate>

        <Style TargetType="{x:Type clrtest:TextFrameControl}">
            <Setter Property="ContentTemplate" Value="{StaticResource EditModeTemplate}"></Setter>
        </Style>

    </UserControl.Resources>

</UserControl>

然后我有一些使用 TextFrameControl 的 XAML:

<Window x:Class="TextBoxColourTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:textBoxColourTest="clr-namespace:TextBoxColourTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <textBoxColourTest:TextFrameControl x:Name="TextFrameControl"></textBoxColourTest:TextFrameControl>
            <Button Content="Red" Click="OnMouseUpRed"></Button>
            <Button Content="Green" Click="OnMouseUpGreen"></Button>
        </StackPanel>
    </Grid>
</Window>

最后是我为按钮更改前景色的事件处理程序背后的代码:

    namespace TextBoxColourTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnMouseUpRed(object sender, RoutedEventArgs routedEventArgs)
        {
            TextFrameControl.Foreground = new SolidColorBrush(Colors.Red);
        }

        private void OnMouseUpGreen(object sender, RoutedEventArgs routedEventArgs)
        {
            TextFrameControl.Foreground = new SolidColorBrush(Colors.Green);
        }
    }
}

当单击其中一个颜色按钮时,前景色不会改变。

如果我更改代码以便可以更改字体系列或字体大小属性的值,那么它确实有效。另外,我发现如果我用TextBlock 替换TextBox,那么颜色确实会改变。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    将 TextBox 的 Foreground 属性绑定到 UserControl 的属性:

    <DataTemplate x:Key="EditModeTemplate">
        <TextBox Text="Hello"
            Foreground="{Binding Foreground,
                         RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </DataTemplate>
    

    【讨论】:

    • 这行得通,但它是如何工作的?任何指针将不胜感激。我不明白为什么我可以更改字体系列和大小,或者如果我用 TextBlock 替换 TextBox,我可以更改前景色。
    • 不知道为什么 Foreground 值是由 TextBlock 继承的,而不是由 TextBox 继承的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2010-11-02
    • 2013-05-29
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多