标签: attached-properties

Silverlight 4默认按钮服务

几个月来,我在SL 3应用程序中成功使用了David Justices Default Button示例.此方法基于附加属性.

升级到SL4后,该方法不再有效,我得到一个XAML异常:

未知的解析器错误:扫描程序2148474880

有人在SL4中成功使用过这个(或任何其他)默认按钮附加行为吗?

有没有其他方法可以在SL4中使用可用的新类实现默认按钮行为?

谢谢,马克

defaultbutton attached-properties silverlight-4.0

5
推荐指数
2
解决办法
6819
查看次数

具有自定义附加属性的 SortDescription

在 Xaml 中,我可以使用 local:TestClass.TestProperty="1" 设置自定义附加属性

我可以使用 {Binding Path=(Namespace:[OwnerType].[PropertyName])} {Binding Path=(local:TestClass.TestProperty)} 绑定到自定义附加属性

但是,当我需要在 SortDescription 中使用自定义附加属性时,如何指定命名空间?我可以使用 new SortDescription("(Grid.Row)", ListSortDirection.Descending) 绑定到附加属性,但在这里我无法在任何地方设置命名空间......

最好的问候,杰斯珀

wpf attached-properties

5
推荐指数
1
解决办法
1889
查看次数

如何绑定到 XAML、WPF 中的附加属性

我想将 User.Password 属性绑定到 PasswordBox (TwoWay)。由于PasswordBox.Password 不可绑定,我制作了 AttachedProperties 来解决此问题(一个用于激活绑定,一个用于保存实际密码)。问题是它们不会绑定(GetBindingExpression 返回 null)。

还:

  • AttachedProperties 起作用。如果我在密码框中键入内容,则密码和密码值(附加属性)会正确设置,但 User.Password 仍为空。
  • 绑定 AttachedProperty 也可以工作,但只有相反:如果我将 PasswordValue 绑定到 TextBlock(TextBlock.Text 是目标,helper:PasswordValue 是源),它就可以工作。只是我不能使用它,因为 User 的属性不是依赖对象。
  • User.Password 是可绑定的(User 实现 INotifyPropertyChanged),并且我设法将 User.Username 绑定到 TextBox.Text 并且(用户名和密码是类似的字符串属性)

以下是附加属性:

public static bool GetTurnOnBinding(DependencyObject obj)
        {
            return (bool)obj.GetValue(TurnOnBindingProperty);
        }

        public static void SetTurnOnBinding(DependencyObject obj, bool value)
        {
            obj.SetValue(TurnOnBindingProperty, value);
        }

        // Using a DependencyProperty as the backing store for TurnOnBinding. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TurnOnBindingProperty =
            DependencyProperty.RegisterAttached(
            "TurnOnBinding",
            typeof(bool),
            typeof(PasswordHelper), …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf binding attached-properties

5
推荐指数
1
解决办法
7208
查看次数

附属物业订单

附加属性应用于对象的顺序是什么?我想我应该忽略这一点,但在我的场景中:我有一个附加属性将VM粘贴到View,然后是另一个依赖于第一个的附加属性.我试图看看如果第二个设置在第一个之前会发生什么,但我无法设法得到错误!即第一个(模型)总是在第二个之前设置,无论xaml中的顺序如何.谁在推动分配令?我可以改变吗?

现在我通过订阅proeprty更改事件来处理迟到的声明:

DependencyPropertyDescriptor dd = DependencyPropertyDescriptor.FromProperty(FrameworkElement.DataContextProperty,depo.GetType());
            dd.AddValueChanged(depo, (s, a) =>
            {
                ChangeDatacontext(s as DependencyObject);
            }
Run Code Online (Sandbox Code Playgroud)

为了模拟问题,我手动设置了一个新的datacontext到对象.

谢谢,菲利克斯

wpf attached-properties

5
推荐指数
1
解决办法
492
查看次数

在 Silverlight 中使用依赖属性时出现“类型中未找到可附加属性”错误

我正在尝试做一些示例应用程序以在 DataGrid 中使用依赖属性,但是当我尝试运行应用程序时,我遇到了运行时异常

在“CustomDependencyProperty”类型中找不到可附加属性“SelectedColumnIndex”。[行:17 位置:74]

这是我用来声明我的依赖属性的代码

public class CustomDependencyProperty : DataGrid
{

    public static DependencyProperty SelectedColumnIndexProperty = DependencyProperty.Register("SelectedColumnIndex",
                                                                                                 typeof(object),
                                                                                                 typeof(DataGrid),
                                                                                                 new PropertyMetadata(0));

    public int SelectedColumnIndex
    {
        get
        {
            return (int)GetValue(SelectedColumnIndexProperty);
        }

        set
        {
            SetValue(SelectedColumnIndexProperty, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 XAML 代码

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="BindingDictionary.MainPage"
    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"
             xmlns:local="clr-namespace:BindingDictionary"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
    </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid x:Name="dataGrid"
                      AutoGenerateColumns="True"
                      ItemsSource="{Binding Responses}" 
                      local:CustomDependencyProperty.SelectedColumnIndex="{Binding Index,Mode=TwoWay}">
        </sdk:DataGrid>
        <TextBlock x:Name="DisplayIndex" Text="{Binding Index}" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚到底是什么问题。我声明依赖属性的方式有什么问题吗?

请帮忙。

谢谢,亚历克斯

silverlight dependency-properties mvvm attached-properties silverlight-4.0

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

无法从代码中将Storyboard.TargetProperty设置为Metro风格应用中的CompositeTransform.Rotation

我正在使用Metro XAML应用程序搞乱一些故事板.我必须创建一个Storyboard代码.我想设置Storyboard.TargetPropertyCompositeTransform.Rotation

这似乎不可能......

我在XAML中的故事板看起来像这样:

<Storyboard>
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="grid">
        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="60"/
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)

我想创造类似的东西.
重要提示:我不是要重新创建这个确切的故事板.我在自定义的代码中ContentControl,所以thisControl,并且没有"网格"来定位动画.目标是控制本身,它已经CompositeTransform预先设定.

到目前为止我的代码是这样的:

var turnSB = new Storyboard();

var doubleAnim = new DoubleAnimationUsingKeyFrames();
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(0), Value = currentAngle });
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(500), Value = targetAngle });

turnSB.Children.Add(doubleAnim);

Storyboard.SetTarget(doubleAnim, this.RenderTransform);
Storyboard.SetTargetProperty(doubleAnim, "(CompositeTransform.Rotation)");

turnSB.Begin();
Run Code Online (Sandbox Code Playgroud)

一旦它遇到Begin方法,我得到一个Exception,说无法解决(CompositeTransform.Rotation).所以我猜我没有完全正确的属性路径.我尝试了不同的变化,但根据PropertyPaths,这应该是正确的,不应该吗?:S

如果这是一个无法解决的问题,我愿意接受有关解决方法的建议......

编辑:

我想我现在已经解决了这个问题.我有一些有趣的发现......

如果我制作一个UserControl,我几乎可以做任何事情.一切正常,我可以设置Storyboard.Targetproperty,动画播放正确.

但是,如果我使用自定义控件,或从其他控件(比如ContentControl)继承,我无法从代码启动Storyboard,仅在某些情况下.

例如:如果我创建一个故事板(在XAML中定义)来为Rotation(或任何转换属性)设置动画并尝试从代码开始,我会得到上述异常.但是,如果我为一个简单的属性设置动画,比如Opacity,那就可以了.
(我对UserControl做了同样的事情,并且它有效.)

有人可以解释一下吗?

storyboard attached-properties microsoft-metro winrt-xaml

5
推荐指数
1
解决办法
3014
查看次数

获取 GridViewColumn 的父级

有没有办法获取 GridViewColumn 的父级(ListView)?

我尝试过 LogicalTreeHelper 和 VisualTreeHelper,但没有骰子。

我可以分享一个有点有趣的你尝试过的东西,它有效但丑陋并不能描述它:

public class Prototype
{
    [Test, RequiresSTA]
    public void HackGetParent()
    {
        var lw = new ListView();
        var view = new GridView();
        var gvc = new GridViewColumn();
        view.Columns.Add(gvc);
        lw.View = view;
        var ancestor = new Ancestor<ListView>();

        var binding = new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListView), 1),
            Converter = new GetAncestorConverter<ListView>(), // Use converter to hack out the parent
            ConverterParameter = ancestor // conveterparameter used to return the parent
        };
        BindingOperations.SetBinding(gvc, GridViewColumn.WidthProperty, binding); …
Run Code Online (Sandbox Code Playgroud)

c# wpf gridview attached-properties

5
推荐指数
1
解决办法
1815
查看次数

将ContextMenu属性绑定到所有者属性

我将上下文菜单绑定到文本框的附加属性时遇到问题.所以我有TextBox,右键单击它有一个上下文菜单.那么如何将上下文菜单的属性绑定到WPF XAML中TextBox的附加属性?在这里,我尝试绑定到TextBox但它没有帮助

 <Style x:Key="DefaultTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="{DynamicResource ThemeSecondary}"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu x:Name="uiContexMenu">
                    <ContextMenu.ItemsSource>
                        <CompositeCollection>
                            <MenuItem Command="Cut" Header="Cut">
                                <MenuItem.Icon>
                                    <Viewbox Width="16" Height="16">
                                        <TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
                                    </Viewbox>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Command="Copy" Header="Copy">
                                <MenuItem.Icon>
                                    <Viewbox Width="16" Height="16">
                                        <TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
                                    </Viewbox>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Command="Paste" Header="Paste">
                                <MenuItem.Icon>
                                    <Viewbox Width="16" Height="16">
                                        <TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
                                    </Viewbox>
                                </MenuItem.Icon>
                            </MenuItem>
                            <CollectionContainer Collection="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}, Path=Extensions.ExtendCommands}"/>
                        </CompositeCollection>
                    </ContextMenu.ItemsSource>
                </ContextMenu>
            </Setter.Value>
        </Setter>
Run Code Online (Sandbox Code Playgroud)

我的附属物:

 #region ExtendCommands dependency …
Run Code Online (Sandbox Code Playgroud)

wpf xaml binding contextmenu attached-properties

5
推荐指数
1
解决办法
573
查看次数

使用自定义面板问题将附加属性绑定到ItemsControl中的项目

我不能让以下XAML按我的意愿工作.所有绑定都起作用,因为绑定没有错误.但是我没有从RatioShape矩形的绑定中获得预期的结果.问题是附加属性wpflib:RatioPanel.Ratio始终返回其默认值,而不是数据绑定值.

所以我认为RatioShape上的附加属性设置在错误的"上下文"中.如何绑定到附加属性,以便wpflib:RatioPanel获取绑定值?

<wpflib:RatioContentPresenter2 RatioMaxValue="{Binding Path=RatioMaxValue}">
    <ItemsControl Grid.Row="0" wpflib:RatioContentPresenter2.RatioOffset="{Binding Path=RatioOffset}" wpflib:RatioContentPresenter2.RatioValue="{Binding Path=RatioValue}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                    <wpflib:RatioPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle x:Name="RatioShape" wpflib:RatioPanel.Ratio="{Binding Path=Value}" Fill="{Binding Path=Brush}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemsSource>
            <Binding  Path="RatioItems" Mode="OneWay" />
        </ItemsControl.ItemsSource>
    </ItemsControl>
</wpflib:RatioContentPresenter2>
Run Code Online (Sandbox Code Playgroud)

wpf xaml binding attached-properties

4
推荐指数
1
解决办法
2669
查看次数

使附加属性在 DataTemplate 中工作

我得到了一个ItemsControl使用Canvasas 的项目ItemsPanel,它的项目根据绑定类型呈现为不同的 WPF 形状,基本上如下所示:

<ItemsControl  ItemsSource="{Binding PreviewShapes}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type local:UiPreviewLineViewModel}">
            <Line X1="{Binding Start.X}" Y1="{Binding Start.Y}"
                        X2="{Binding End.X}" Y2="{Binding End.Y}" 
                        StrokeThickness="0.75" Stroke="{Binding Brush}" x:Name="Line" ToolTip="{Binding Text}">
            </Line>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:UiPreviewEllipsisViewModel}">
            <Ellipse Canvas.Left="{Binding UpperLeft.X" Canvas.Top="{Binding UpperLeft.Y}" 
                     Width="{Binding Width}" Height="{Binding Height}" 
                     StrokeThickness="0.75" Stroke="{Binding Brush}" x:Name="Ellipse" ToolTip="{Binding Text}">
            </Ellipse>
        </DataTemplate>
    </ItemsControl.Resources>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas IsItemsHost="True" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="SketchCanvas" ClipToBounds="False">
            </Canvas>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

因此,我基本上将对象添加到PreviewShapes视图模型中,并根据它们渲染到 WPFLineEllipses 的类型。这基本上可以工作,但附加的属性Canvas.Left和 …

wpf datatemplate attached-properties

4
推荐指数
1
解决办法
1890
查看次数