2014-02-27 30 views
0

綁定風格這是我的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> 

我的控制將不顯示「搜索」沒有被選中文本框或包含任何東西。

回答

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}" /> 
+0

我想這一點,但同樣沒有成功:/我的用戶控件的xaml代碼是這樣的:http://pastebin.com/A8fxAhSQ,它的vb代碼是這樣的:http://pastebin.com/bhpAe55U。我嘗試在其他應用程序中實現控制,方法是將它添加到資源中,這是代碼:http://pastebin.com/6xR40RZL – Krowi