2015-10-28 68 views
0

我有以下XAML,並試圖在兩個標籤裏面實現屬性,所以我可以在控件在XAML中實例化時設置它們的標題。在稍後階段,這也將來自代碼。請你能告訴我,我對數據綁定有什麼想法,以及我該怎麼做?我對WPF很陌生。綁定的誤解沒有得到屬性設置

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication1" 
     xmlns:ctrl="clr-namespace:xCtrl" 
     Title="MainWindow" Width="525" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" WindowStyle="None"> 
    <Window.Resources> 
     <p:Style TargetType="ctrl:BSHintButton" x:Key="BSStyle"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 

         <Grid x:Name="xgrid" Height="50" Width="500" VerticalAlignment="Top"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="25"/> 
           <RowDefinition Height="25"/> 
          </Grid.RowDefinitions> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="70"/> 
           <ColumnDefinition Width="430 
                 "/> 
          </Grid.ColumnDefinitions> 
          <Label x:Name="xLabel" Content="{Binding Caption}" 
            Grid.Column="1" HorizontalAlignment="Left" 
            Margin="10,8,0,0" VerticalAlignment="Top" 
            Grid.RowSpan="2" Width="Auto" 
            FontFamily="Calibri" FontSize="14" 
            FontWeight="Bold" Height="Auto"/> 
          <Label x:Name="xHint" Content="{Binding HintText}" 
            Foreground="DarkCyan" Grid.Column="1" 
            HorizontalAlignment="Left" Height="Auto" 
            Margin="10,-2,0,0" Grid.Row="1" 
            VerticalAlignment="Top" Width="Auto" 
            FontFamily="Calibri" FontSize="14"/> 
          <Rectangle x:Name="xFocus" 
           Stroke="Orange" 
           StrokeThickness="1" 
           RadiusX = "4" RadiusY="4" Grid.RowSpan="2" Grid.ColumnSpan="2" IsEnabled="True" Visibility="Hidden"  
             /> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsMouseOver" 
        Value="True"> 
           <Setter TargetName="xgrid" Property="Background"> 
            <Setter.Value> 
             <LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.01"> 
              <GradientStop Color="White" Offset="0"/> 
              <GradientStop Color="Gold" Offset="0.1"/> 
              <GradientStop Color="White" Offset="0.8"/> 
             </LinearGradientBrush> 

            </Setter.Value> 
           </Setter> 
           <Setter TargetName="xFocus" Property="Visibility" 
        Value="Visible" /> 

          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <Style TargetType="ctrl:OfficeBSHintButton" BasedOn="{StaticResource BSStyle}" /> 

    </Window.Resources> 


    <StackPanel Orientation="Vertical" Margin="0,0,0,0.001"> 
     <ctrl:BSHintButton x:Name="Button1" Tag ="2" Click="Button1_Click" Caption="caption1" HintText="Hint1"> 
     </ctrl:BSHintButton> 
     <ctrl:BSHintButton x:Name="Button2" Tag ="1" Click="Button1_Click" Caption="caption1" HintText="Hint2"> 
     </ctrl:BSHintButton> 
    </StackPanel> 
</Window> 

代碼類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 

namespace xCtrl 
{ 
    public class BSHintButton : Button 
    { 
     public static readonly DependencyProperty CaptionProperty = 
     DependencyProperty.Register("Caption", 
            typeof(string), 
            typeof(Label) 
            ); 
     public static readonly DependencyProperty HintTextProperty = 
     DependencyProperty.Register("HintText", 
            typeof(string), 
            typeof(Label) 
            ); 
     public string Caption 
     { 
      get { return (string)GetValue(CaptionProperty); } 
      set { SetValue(CaptionProperty, value); } 
     } 
     public string HintText 
     { 
      get { return (string)GetValue(HintTextProperty); } 
      set { SetValue(HintTextProperty, value); } 
     } 

    } 

} 
+1

綁定適用於'DataContext'(models等),其中'TemplateBinding'適用於模板化控件(按鈕)。你應該使用'{TemplateBinding Caption}'。 – Silvermind

+0

如果我更改爲TemplateBinding,它不知道我在說什麼,因爲它無法找到Caption,HintText的成員。此外,它無法找到按鈕上的DependencyProperty的thst statc成員。 – user2903089

+0

我只是在INotifyPropertyChanged,但那也不管用,現在很沮喪。我認爲是因爲來自風格? – user2903089

回答

2

沒有a good, minimal, complete code example,它可能很難或不可能確切知道問題是什麼,沒關係測試解決方案。但是查看你的代碼,我發現最明顯的錯誤是你正在聲明你的依賴屬性不正確。

依賴項屬性的所有者是聲明屬性的類型,而不是您期望用作綁定目標的類型。

所以,你的代碼看起來應該是這樣,而不是:

public static readonly DependencyProperty CaptionProperty = 
    DependencyProperty.Register("Caption", 
           typeof(string), 
           typeof(BSHintButton) 
           ); 
    public static readonly DependencyProperty HintTextProperty = 
    DependencyProperty.Register("HintText", 
           typeof(string), 
           typeof(BSHintButton) 
           ); 

有錯誤的所有者可以防止WPF從正確處理在XAML申報約束力。這是我的希望和期望,修復上面的代碼將允許綁定工作。

如果不是,請編輯您的問題,以便它包含一個很好的代碼示例。沒有人,就不可能在「原位」上舉例說明具體問題,以便正確調試。

說到調試,您還應該養成檢查程序調試輸出的習慣。當綁定不起作用時,通常WPF已經向調試輸出發出了一個或多個錯誤消息。有些時候,這些信息甚至是有用的。 :)