English 中文(简体)
使用习俗时无效 财产
原标题:UIElement null when using custom DependencyProperty

我为工具的使用建立了简单的控制:

public partial class TooltipControl : UserControl
{
    public static readonly DependencyProperty ToolTipControlContentProperty
        = DependencyProperty.Register("ToolTipControlContent", typeof(FrameworkElement), typeof(TooltipControl), 
            new PropertyMetadata(new Grid()));


    public static readonly DependencyProperty ToolTipProperty
      = DependencyProperty.Register("ToolTip", typeof(string), typeof(TooltipControl),
          new PropertyMetadata(string.Empty));

    public string Tooltip
    {
        get { return GetValue(ToolTipProperty) as string; }
        set { SetValue(ToolTipProperty, value); }
    }

    public FrameworkElement ToolTipControlContent
    {
        get { return GetValue(ToolTipControlContentProperty) as FrameworkElement; }
        set { SetValue(ToolTipControlContentProperty, value); }
    }

    public TooltipControl()
    {
        InitializeComponent();
    }

}

This is it s xaml:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
   x:Class="Dashboard.Controls.TooltipControl" 
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Width="150" Height="75">

        <Border Child="{Binding ToolTipControlContent}">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                    <GradientStop Color="#FF1BA1E2" Offset="0"/>
                    <GradientStop Color="#FF096A99" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
        </Border>

    </Grid>
</UserControl>

我在主食中利用这一控制。 xaml:

<local:TooltipControl Width="150" Height="75" x:Name="tileWidthSettings" Tooltip="Adjust the tile width of widgets" Margin="3,0,0,0" VerticalAlignment="Top">
    <local:TooltipControl.ToolTipControlContent>
        <Grid Height="75" Width="150">

            <Grid.Effect>
                <DropShadowEffect BlurRadius="8" Direction="0" ShadowDepth="0"/>
            </Grid.Effect>
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF1BA1E2" Offset="0.004"/>
                    <GradientStop Color="#FF096A99" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <StackPanel d:LayoutOverrides="Height" VerticalAlignment="Center" Margin="15,0">
                <TextBlock TextWrapping="Wrap" Text="Tile width" FontFamily="Trebuchet MS" HorizontalAlignment="Center" Foreground="White" FontSize="13.333"/>
                <telerik:RadSlider x:Name="radSliderTileWidth" ValueChanged="radSliderTileWidth_ValueChanged" RepeatInterval="10" Value="300" Minimum="0" Margin="0" />
            </StackPanel>
        </Grid>
    </local:TooltipControl.ToolTipControlContent>
</local:TooltipControl>

如果我直接使用:

radSliderTileWidth.Minimum = 1.0;

缩略语 我拿到了NUL。 尽管我可以通过姓名直接接触这个要素,但总是全国人民力量。 我在Loaded活动手里这样做。

然后,我尝试:

(tileWidthSettings.ToolTipControlContent.FindName("radSliderTileWidth" ) as RadSlider).Maximum = GetScreenWidth() / 2;

它仍然是民族解放军。

但是,如果我有时随意尝试的话,它就会发挥作用! 或许是因为在装货活动中采用了用户控制吨的模板。

然后,我尝试:

RadSlider slider = tileWidthSettings.ToolTipControlContent.FindChildByType<RadSlider>();

这种方法基本上是推广方法,使用视力TreeHelper进行儿童搜查。

因此,我在这里做了什么错误呢? 我怎么能以统一的方式在Loaded事件或在其他事件处理者中工作?

此前,我正在使用银星4.0,但我认为这是一个也适用于世界森林论坛的一般概念。 因此,我与世界森林论坛一样对它表示反对。

问题回答

FrameworkElement.Loaded,银灯,“Loaded and Control Object Lifetime”部分:

在银星举行的Loaded活动的时间不同于框架要素的时间安排。 世界森林论坛的转播活动。 具体来说,WPF Loaded活动是在使用模板之后发生的。 在银星,在使用模板后,没有保证举行Loaded活动。 如果你利用Loaded活动进行相对常见的控制情景,这或许是你们面临的问题:你想研究视像树,要么获得其他东西方的价值,要么改变模版构成的价值,只要你知道新价值的话。 在这种情况下,如果你直接从一名Loaded手勒那里打上模板内容的直观树,那么向银光视仪系统传输器打电话以检查模板内容的图像树。

......





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签