标签: attached-properties

InitializeComponent()做什么,它在WPF中如何工作?

InitializeComponent()什么,以及它在WPF中如何运作?

首先,我会特别感兴趣的是了解构造顺序的血腥细节,以及附加属性时会发生什么.

.net c# wpf attached-properties

156
推荐指数
2
解决办法
15万
查看次数

如何在代码后面访问附加属性?

我的XAML中有一个矩形,并希望Canvas.Left在后面的代码中更改其属性:

<UserControl x:Class="Second90.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" KeyDown="txt_KeyDown">
    <Canvas>
        <Rectangle 
            Name="theObject" 
            Canvas.Top="20" 
            Canvas.Left="20" 
            Width="10" 
            Height="10" 
            Fill="Gray"/>
    </Canvas>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

private void txt_KeyDown(object sender, KeyEventArgs e)
{
    theObject.Canvas.Left = 50;
}
Run Code Online (Sandbox Code Playgroud)

有谁知道这样做的语法是什么?

c# wpf code-behind attached-properties

94
推荐指数
3
解决办法
5万
查看次数

WPF中依赖属性和附加属性之间的区别是什么?

WPF中的(自定义)依赖项属性和附加属性之间有什么区别?每种用途有哪些用途?这些实现通常有何不同?

.net wpf dependency-properties attached-properties

87
推荐指数
3
解决办法
3万
查看次数

什么时候应该使用FrameworkPropertyMetadata或UIPropertyMetadata而不是普通的PropertyMetadata?

在查看样本附加的属性和行为时,我已经看到了使用的混合物FrameworkPropertyMetadata,UIPropertyMetadataPropertyMetadata.由于它们都构成了一个继承层次结构,我该如何选择使用哪一个?

wpf attachedbehaviors attached-properties

79
推荐指数
1
解决办法
2万
查看次数

WPF附加属性数据绑定

我尝试使用附加属性绑定.但无法让它发挥作用.

public class Attached
{
    public static DependencyProperty TestProperty =
        DependencyProperty.RegisterAttached("TestProperty", typeof(bool), typeof(Attached),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Inherits));

    public static bool GetTest(DependencyObject obj)
    {
        return (bool)obj.GetValue(TestProperty);
    }

    public static void SetTest(DependencyObject obj, bool value)
    {
        obj.SetValue(TestProperty, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML代码:

<Window ...>
    <StackPanel local:Attached.Test="true" x:Name="f">
        <CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay, RelativeSource={RelativeSource Self}}" />
        <CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay}" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

和绑定错误:

System.Windows.Data Error: 40 : BindingExpression path error: '(local:Attached.Test)' property not found on 'object' ''StackPanel' (Name='f')'. BindingExpression:Path=(local:Attached.Test); DataItem='StackPanel' (Name='f'); …
Run Code Online (Sandbox Code Playgroud)

wpf xaml binding attached-properties

63
推荐指数
2
解决办法
4万
查看次数

如何使用样式中的附加属性?

我在ButtonStyle中创建了一个Image.现在我创建了一个附加属性,以便我可以为该图像设置Source.应该是直截了当但我坚持下去.

这是我缩短的ButtonStyle:

<Style x:Key="ToolBarButtonStyle"
        TargetType="Button">
    ...
    <Image x:Name="toolbarImage"
            Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}"
            Width="48"
            Height="48" />
    ...
</Style>
Run Code Online (Sandbox Code Playgroud)

这是附加的属性定义,注意我不知道如何修复回调,因为dependencyproperty似乎是按钮而不是图像.而Button不会在我的风格中暴露我的Image.这很棘手.

namespace SalesContactManagement.Infrastructure.PrismExt
{
    public class ImgSourceAttachable
    {
        public static void SetImgSource(DependencyObject obj, string imgSource)
        {
            obj.SetValue(ImgSourceProperty, imgSource);
        }

        public static string GetImgSource(DependencyObject obj)
        {
            return obj.GetValue(ImgSourceProperty).ToString();
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImgSourceProperty =
            DependencyProperty.RegisterAttached("ImgSource", typeof(string), typeof(ImgSourceAttachable), new PropertyMetadata(Callback));

        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //((Button)d).Source = …
Run Code Online (Sandbox Code Playgroud)

.net silverlight wpf dependency-properties attached-properties

35
推荐指数
1
解决办法
2万
查看次数

附加属性在WPF中的工作原理是什么?

关于Attached Properties如何将其值传递给父元素或子元素,我有点神秘.TextElement.FontFamily导致子元素继承分配给该属性的值(看似下游的操作,父级到子级).Grid.Column导致父项在特定位置显示该子项(看似上游操作,子项与父项).Attached Property值如何知道要么向上还是向下流动?我的这个概念是不正确的,还是有一个缺失会让所有这些都被置于透视中?

<StackPanel TextElement.FontFamily="Wingdings">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="1" Content="My Button"/>
    </Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

wpf xaml attached-properties

23
推荐指数
1
解决办法
2万
查看次数

附加属性的模板绑定

我的按钮有一个标准样式,但我希望样式的某些部分是可配置的.例如,当按钮触发MouseOver时,我会出现边框,我希望边框颜色可配置.

下面这篇文章:http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/我以为我可以使用附加属性和TemplateBinding来实现这一目标.

我创建了以下附加属性:

public static class ThemeProperties
{
    public static Brush GetButtonBorderColour(DependencyObject obj)
    {
        return (Brush)obj.GetValue(ButtonBorderColourProperty);
    }

    public static void SetButtonBorderColour(DependencyObject obj, Brush value)
    {
        obj.SetValue(ButtonBorderColourProperty, value);
    }

    public static readonly DependencyProperty ButtonBorderColourProperty =
        DependencyProperty.RegisterAttached(
            "ButtonBorderColour",
            typeof(Brush),
            typeof(ThemeProperties),
            new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.Inherits));
}
Run Code Online (Sandbox Code Playgroud)

我这样设置属性:

<Button Style="{StaticResource RedButton}" local:ThemeProperties.ButtonBorderColour="#B20000"/>
Run Code Online (Sandbox Code Playgroud)

我的风格看起来像这样:

<Window.Resources>
    <Style x:Key="RedButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="FontFamily" Value="Tahoma"/>
        <Setter Property="FontSize" Value="11px"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="MinHeight" Value="25" />

        <Setter Property="FocusVisualStyle" …
Run Code Online (Sandbox Code Playgroud)

wpf templatebinding attached-properties

22
推荐指数
1
解决办法
1万
查看次数

类型列表的附加属性

我想创建一个可以使用此语法的附加属性:

<Button>
  <Image .../>
  <ui:ToolbarItem.DisplayFilter>
    <TabItem .../>
    <TabItem .../>
    <TabItem .../>
  </ui:ToolbarItem.DisplayFilter>
</Button> 
Run Code Online (Sandbox Code Playgroud)

这是我尝试这样做的:

public class ToolbarItem
{
  /// <summary>
  /// Identifies the DisplayFilter attached property. 
  /// </summary>
  public static readonly DependencyProperty DisplayFilterProperty =
    DependencyProperty.RegisterAttached(
     "DisplayFilter",
     typeof( IList ),
     typeof( ToolbarItem )
    );

  public static IList GetDisplayFilter( Control item ) {
    return (IList)item.GetValue( DisplayFilterProperty );
  }

  public static void SetDisplayFilter( Control item, IList value ) {
    item.SetValue( DisplayFilterProperty, value );
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,这会在分析时导致异常 - System.ArgumentException:TabItem不是属性'DisplayFilter'的有效值.那么如何配置我的附加属性以便我可以使用所需的XAML语法?

wpf attached-properties

19
推荐指数
1
解决办法
7475
查看次数

C#WPF附加属性 - 错误:"XML名称空间中不存在该属性"

我需要为现有的WPF控件(Groupbox,文本框,复选框等)创建一个新属性,这个控件将存储其访问级别,因此我找到了附加属性.我以此网站为例http://dotnetbyexample.blogspot.com.br/2010/05/attached-dependency-properties-for.html

一切都很好,但是当我尝试在某些控件上使用它时,我得到了以下错误...

错误1 XML命名空间'clr-namespace中不存在属性'DependencyPropertiesHoster.AcessLevel':ImageGUI.App_Code; assembly = ImageGUI'.第131行位置97. ImageGUI\MainWindow.xaml 131 97 ImageGUI

这是我的C#代码片段......

namespace ImageGUI.App_Code
{    
    public static class DependencyPropertiesHoster
    {
    //[AttachedPropertyBrowsableForChildren]
    public static readonly DependencyProperty AcessLevelProperty =
      DependencyProperty.RegisterAttached("AcessLevel",
      typeof(EAcessLevel),
      typeof(DependencyPropertiesHoster),
      new PropertyMetadata(AcessLevelChanged));

    // Called when Property is retrieved
    public static EAcessLevel GetAcessLevel(DependencyObject obj)
    {
        if (obj != null)
            return (EAcessLevel)obj.GetValue(AcessLevelProperty);
        else
            return EAcessLevel.Client;
        //return obj.GetValue(AcessLevelProperty) as EAcessLevel;

    }

    // Called when Property is set
    public static void SetAcessLevel(
       DependencyObject obj,
       EAcessLevel value)
    {
        obj.SetValue(AcessLevelProperty, value);
    }

    // Called when …
Run Code Online (Sandbox Code Playgroud)

c# wpf dependencies dependency-properties attached-properties

16
推荐指数
1
解决办法
4686
查看次数