简体   繁体   English

为什么我的依赖属性会出现异常?

[英]Why am I getting an exception on my dependency property?

Just spent a few hours trying to get my head around dependency properties (finding a lot of valuable info here on the site). 只花了几个小时试图了解依赖属性(在网站上找到很多有价值的信息)。 I've written my very first dependency property, but it is not behaving as I would like it to. 我已经编写了我的第一个依赖属性,但它的行为并不像我希望的那样。 Can anybody have a look at my code and see if he/she can spot whats wrong? 任何人都可以查看我的代码,看看他/她是否能发现错误? When trying to run the app I get a TargetInvocationException was thrown 在尝试运行应用程序时,我收到了一个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;
    }
}

Changing this line of Xaml... < Setter Property="Style" Value="Transparent"/> 改变这行Xaml ... <Setter Property =“Style”Value =“Transparent”/>

To this... 对此......

<!-- <Setter Property="Style" Value="Transparent"/> -->

(ie, comment it out) (即评论出来)

Will avoid the exception. 将避免异常。

Setting a style the way you are doing would entail referencing something already defined in the object tree like {StaticResource Transparent}. 以您的方式设置样式将需要引用已在对象树中定义的内容,如{StaticResource Transparent}。

The inner exception is 'object reference not set...', which makes it clear that the constructor cannot find a reference to "Transparent" the way it has been coded. 内部异常是“对象引用未设置...”,这清楚地表明构造函数无法按照编码的方式找到对“透明”的引用。 Note that the exception is thrown after the TransparentTb is already constructed. 请注意,在构造TransparentTb 之后抛出异常。 You can prove this by setting breakpoints. 您可以通过设置断点来证明这一点。

Also, I checked your TransparentTb code, and it works fine. 另外,我检查了您的TransparentTb代码,它工作正常。 The culprit is your TabItem Style Setter. 罪魁祸首是你的TabItem Style Setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM