繁体   English   中英

WPF触发TextBox.Text无效

[英]WPF trigger for TextBox.Text doesn't work

我的用户控件中有一个名为IsPromptShown的依赖项属性:

public static DependencyProperty IsPromptShownProperty = DependencyProperty.
    Register("IsPromptShown", typeof(bool), typeof(AutoCompleteSearchBox), new 
    PropertyMetadata(true));

public bool IsPromptShown
{
    get { return (bool)GetValue(IsPromptShownProperty); }
    set { SetValue(IsPromptShownProperty, value); }
}

该自定义控件包含一个TextBox 该文本框没有为其Text属性分配任何值:

<TextBox Name="_searchTextBox" Grid.Row="0" VerticalAlignment="Top" 
    GotFocus="SearchTextBox_GotFocus" LostFocus="SearchTextBox_LostFocus" 
    TextChanged="SearchTextBox_TextChanged"/>

现在,我在托管窗口中设置以下触发器:

<Trigger Property="IsPromptShown" Value="True">
    <!--<Setter Property="FontStyle" Value="Italic"/>-->
    <Setter Property="TextBox.Text" Value="Seek"/>
</Trigger>

设置FontStyle的带注释的行有效,但设置TextBox.Text的第二行则无效。 我也一直试图设置Foreground属性,但也失败了。 到底是怎么回事?

当我开始使用WPF时遇到类似的问题。 您只需要以不同的方式看待事物。 与其查看IsPromptShown属性的值并尝试在UserControlTrigger中更改TextBox的属性, IsPromptShown做。 看的价值IsPromptShown财产和尝试改变的属性TextBox在一个TriggerTextBox

<TextBox Name="_searchTextBox" Grid.Row="0" VerticalAlignment="Top" 
    GotFocus="SearchTextBox" LostFocus="SearchTextBox_LostFocus" 
    TextChanged="SearchTextBox_TextChanged">
    <TextBox.Style>
        <Style>
            <Setter Property="TextBox.Text" Value="Default value if required" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsPromptShown, ElementName=This}" 
                    Value="True">
                    <Setter Property="TextBox.Text" Value="Seek" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

请注意,要使此功能起作用,您需要将Name=This添加到UserControl的声明中。 这只是让Framework知道在哪里可以找到IsPromptShown属性...如果愿意,您可以轻松地为此使用RelativeSource Binding

我认为问题是您无法从外部访问UserControl中的文本框的TextProperty!

尝试创建一个Text DependencyProperty,该属性在UserControl中设置文本框的值,并在触发器中设置此属性!

我找到了解决方案。 这不是小事,但可行。 假设我们有一个自定义UserControl ,其中包含一个TextBox和ather控件。 我们希望能够根据某些bool UserControl.IsSomething依赖项属性为每个内部控件分配样式。 首先,我们必须声明另一个依赖项属性Style UserControl.AlternativeStyle 然后,我们将事件处理程序附加到IsSomething ,以便在IsSomething更改时切换当前的StyleAlternativeStyle

public static DependencyProperty AlternativeStyleProperty =
    DependencyProperty.Register(
        "AlternativeStyle",
        typeof(Style),
        typeof(MyUserControl));

public Style AlternativeStyle
{
    get { return (Style)GetValue(AlternativeStyleProperty); }
    set { SetValue(AlternativeStyleProperty, value); }
}

public static DependencyProperty IsSomethingProperty =
    DependencyProperty.Register(
        "IsSomething",
        typeof(bool),
        typeof(MyUserControl),
        new PropertyMetadata(true, IsSomethingProperty_Changed));

public bool IsSomething
{
    get { return (bool)GetValue(IsSomethingProperty); }
    set { SetValue(IsSomethingProperty, value); }
}

private static void IsSomethingProperty_Changed(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    // Swap styles
    // e.g.
    // var tempStyle = Style;
    // Style = AlternativeStyle;
    // AlternativeStyle = tempStyle;
}

最难的部分是在自定义UserControl外部设置AlternativeStyle 下面是如何在托管我们的自定义控件的窗口的XAML中实现这一点:

<Window.Resources>
    <Style x:Key="DefaultTextBoxStyle" TargetType="TextBox">
        <Setter Property="Background" Value="Red"/>
    </Style>
    <Style x:Key="DefaultUserControlStyle" TargetType="local:MyUserControl">
        <Style.Resources>
            <Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">
                <Setter Property="Background" Value="Blue"/>
            </Style>
        </Style.Resources>
    </Style>
    <Style x:Key="AlternativeUserControlStyle" TargetType="local:MyUserControl" BasedOn="{StaticResource DefaultUserControlStyle}">
        <Style.Resources>
            <Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">
                <Setter Property="Background" Value="Green"/>
            </Style>
        </Style.Resources>
    </Style>
    <Style TargetType="local:MyUserControl" BasedOn="{StaticResource DefaultUserControlStyle}">
        <Setter Property="AlternativeStyle" Value="{StaticResource AlternativeUserControlStyle}"/>
    </Style>
</Window.Resources>

上面的样式将自动添加到MyUserControl所有实例,并且当我们更改IsSomething值时,替代样式将应用于更改后的IsSomething的所有者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM