用户控件的绑定样式

时间:2014-02-27 17:29:56

标签: wpf vb.net binding user-controls

这是我的xaml代码:

<UserControl x:Class="UserControl1"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                <VisualBrush.Visual>
                    <Label Content="{Binding Path=CueBannerText}" Foreground="LightGray" />
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid>
    <TextBox TextWrapping="Wrap"/>
</Grid>
</UserControl>

如果我在项目中调用控件,我想通过代码绑定标签内容。我这样做是这样的:

Public Class UserControl1 
Public Property CueBannerText As String
    Get
        Return _oText
    End Get
    Set(value As String)
        _oText = value
    End Set
End Property

Private _oText As String = "Search"

Public Sub New()
    InitializeComponent()
    Me.DataContext = Me
End Sub
End Class

如果我在代码中调用我的控件使用:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
    xmlns:Noru="clr-namespace:NoruTextBox;assembly=NoruTextBox">
<Grid>
    <Noru:UserControl1></Noru:UserControl1>
</Grid>
</Window>

我的控件不会显示“搜索”,因为TextBox未被选中或包含任何内容。

1 个答案:

答案 0 :(得分:0)

要执行您想要的操作,您需要在后面的UserControl代码中实现INotifyPropertyChanged Interface,或者声明DependencyProperty代替:

Public Shared CueBannerTextProperty As DependencyProperty = DependencyProperty.
    Register("CueBannerText", GetType(String), GetType(TestView), 
    New PropertyMetadata("Search"))

Public Property CueBannerText() As String
    Get
        Return DirectCast(GetValue(CueBannerTextProperty), String)
    End Get
    Set
        SetValue(CueBannerTextProperty, value)
    End Set
End Property
  

免责声明:我刚使用在线转换器将其转换为VB,因此我无法确认其正确性。

使用DependencyProperty还可以让您在StyleAnimation(在这种情况下不太可能)或Binding中设置此值:

<Noru:UserControl1 CueBannerText="{Binding SomeValue}" />