剛花了幾個小時試圖讓我的頭依賴屬性(在網站上找到很多有價值的信息)。我已經編寫了我的第一個依賴屬性,但它並不像我希望的那樣工作。任何人都可以看看我的代碼,看看他/她是否可以發現什麼是錯的? 當試圖運行應用程序,我得到一個TargetInvocationException被拋出爲什麼我在依賴項屬性上收到異常?
<Window x:Class="TextEditorMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TextEditorMVVM.ViewModel"
Title="MainWindow" Height="660" Width="621" ResizeMode="CanResize" Background="Gray">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Resources/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<c:TextEditorViewModel x:Key="TextEditorViewModel"></c:TextEditorViewModel>
</ResourceDictionary>
</Window.Resources>
<Border CornerRadius="10" BorderThickness="12" Background="#FF505050">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="LightGray" Offset="1" />
<GradientStop Color="Gray" Offset="0" />
</LinearGradientBrush>
</Border.BorderBrush>
<StackPanel DataContext="{StaticResource TextEditorViewModel}">
<Menu Height="auto" Background="Transparent" Foreground="White">
<MenuItem Width=" auto" Height="auto" Header="_File" VerticalAlignment="Center">
<MenuItem Header="_New" Command="{Binding CreateNewTabCommand}"></MenuItem>
<MenuItem Header="_Open" Command="{Binding OpenFileCommand}"></MenuItem>
<MenuItem Header="_Save" Command="{Binding SaveFileCommand}"></MenuItem>
<MenuItem Header="_Close" Command="{Binding CloseTabCommand}"></MenuItem>
<MenuItem Header="_Print" Command="{Binding PrintCommand}"></MenuItem>
<MenuItem Header="_Exit" Command="{Binding }"></MenuItem>
</MenuItem>
<MenuItem Width=" auto" Height="auto" Header="_Edit" VerticalAlignment="Center">
<MenuItem Header="_Cut" Command="ApplicationCommands.Cut"></MenuItem>
<MenuItem Header="_Copy" Command="ApplicationCommands.Copy"></MenuItem>
<MenuItem Header="_Paste" Command="ApplicationCommands.Paste"></MenuItem>
</MenuItem>
<MenuItem Width=" auto" Height="auto" Header="_Help" VerticalAlignment="Center">
<MenuItem Header="_Call Mikael" Command="{Binding }"></MenuItem>
<MenuItem Header="_Call Semyon" Command="{Binding }"></MenuItem>
<MenuItem Header="_Cry" Command="{Binding }"></MenuItem>
</MenuItem>
<Expander Header="Autosave" VerticalAlignment="Center" Foreground="White">
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}"></CheckBox>
<TextBox Width="40" Height="20" Margin="4" ></TextBox>
<Label VerticalAlignment="Center" Foreground="White">seconds</Label>
</StackPanel>
</Expander>
</Menu>
<c:TransparentTb IsTransparent="False" Text="Why aren't you working????">
</c:TransparentTb>
<TabControl x:Name="_tabControl" ItemsSource="{Binding TestTab}" SelectedItem="{Binding SelectedTab}" Background="Transparent" BorderThickness="0">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Title}"/>
<Setter Property="Style" Value="Transparent"/>
<Setter Property="Content" Value="{Binding InputText}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</StackPanel>
</Border>
class TransparentTb : TextBlock
{
static TransparentTb()
{
}
{
get { return (bool) GetValue(IsTranparentProperty); }
set { SetValue(IsTranparentProperty, value); }
}
// Using a DependencyProperty as the backing store for IsTranparent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsTranparentProperty =
DependencyProperty.Register("IsTransparent", typeof (bool), typeof (TransparentTb),
new UIPropertyMetadata(false, TransparentTb.IsTransparentPropertyChanged,
TransparentTb.IsTransparentCoerce, false));
private static void IsTransparentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TransparentTb inst = (TransparentTb) d;
if ((bool)e.NewValue == true)
{
inst.Background = Brushes.Transparent;
}
else inst.Background = Brushes.Black;
}
private static object IsTransparentCoerce(DependencyObject d, object value)
{
return value;
}
}
如果可能的話,您可以請包括來自異常的堆棧跟蹤,以便我們有更多的錯誤上下文嗎? –
如何做堆棧跟蹤? –
無論您捕獲並顯示異常,您都可以從異常中顯示StackTrace屬性。這裏是更多的信息:http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx –