對,我有什麼是:我怎樣才能在模板控件綁定的命令,我的ViewModel
<Window x:Class="WpfGettingThingsDone.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True"
Background="Transparent"
WindowStyle="None"
Title="{Binding Title}" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="WindowBorderBackground" TargetType="{x:Type Border}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
<GradientStop Color="#FF222222" Offset="0" />
<GradientStop Color="#FF222222" Offset="0.2" />
<GradientStop Color="#FFAAAAAA" Offset="0.6" />
<GradientStop Color="#FF222222" Offset="0.7" />
<GradientStop Color="#FFAAAAAA" Offset="0.9" />
<GradientStop Color="#FF222222" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="WindowHeaderedContent" TargetType="{x:Type HeaderedContentControl}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Border
Background="Black"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="5,5,0,0"
Padding="4"
SnapsToDevicePixels="True"
>
<DockPanel>
<Button DockPanel.Dock="Right" Command="{Binding Path=CloseCommand}">X</Button>
<TextBlock
FontSize="14"
FontWeight="Bold"
Foreground="White"
HorizontalAlignment="Center"
Text="{TemplateBinding Content}" />
</DockPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Border CornerRadius="5" Style="{StaticResource WindowBorderBackground}">
<HeaderedContentControl Header="Current Contexts"
Style="{StaticResource WindowHeaderedContent}"
>
</HeaderedContentControl>
</Border>
</Window>
基本上繪製一個窗口,一個漂亮的漸變背景,使用HeaderedContentControl創建的標題欄,它使用HeaderTemplate將x按鈕放在那裏。
像這樣:
然而,正如你所看到的,我試過在我的視圖模型結合的X(關閉)按鈕,CloseCommand的命令。假設我的ViewModel是正確的,並且我對WPF數據綁定東西缺乏理解是問題,那麼我做錯了什麼?難道我不能按照我的方式去完成嗎?
(注:對於這個問題的目的,我合併所有的資源在使用中通過窗口進入windows資源字典。)
編輯:因爲山姆建議我的DataContext我窗口沒有設置,我將澄清它已設置,但在創建MainWindow時在App.Xaml的代碼中完成。
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
var viewModel = new MainWindowViewModel();
viewModel.RequestClose += (s, ev) => mainWindow.Close();
mainWindow.DataContext = viewModel;
mainWindow.Show();
}
}
Ta,非常感謝你:) – Sekhat