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); }
}
}
}
綁定適用於'DataContext'(models等),其中'TemplateBinding'適用於模板化控件(按鈕)。你應該使用'{TemplateBinding Caption}'。 – Silvermind
如果我更改爲TemplateBinding,它不知道我在說什麼,因爲它無法找到Caption,HintText的成員。此外,它無法找到按鈕上的DependencyProperty的thst statc成員。 – user2903089
我只是在INotifyPropertyChanged,但那也不管用,現在很沮喪。我認爲是因爲來自風格? – user2903089