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>
我的控制將不顯示「搜索」沒有被選中文本框或包含任何東西。
我想這一點,但同樣沒有成功:/我的用戶控件的xaml代碼是這樣的:http://pastebin.com/A8fxAhSQ,它的vb代碼是這樣的:http://pastebin.com/bhpAe55U。我嘗試在其他應用程序中實現控制,方法是將它添加到資源中,這是代碼:http://pastebin.com/6xR40RZL – Krowi