Attatched Property作为绑定目标不会更新值

时间:2015-06-23 14:19:54

标签: c# wpf

我尝试使用" IsSelected"扩展wpf按钮。属性。我创建了一个具有attatched属性的依赖项对象,如下所示:

class FormSelectorExtension : DependencyObject
{
    public static DependencyProperty IsSelectedProperty =
        DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(FormSelectorExtension),
        new PropertyMetadata(new PropertyChangedCallback(SelectionChanged)));

    public static bool GetIsSelected(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsSelectedProperty);
    }

    public static void SetIsSelected(DependencyObject obj, bool value)
    {
        obj.SetValue(IsSelectedProperty, value);
    }

    private static void SelectionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        Console.WriteLine(obj.ToString());
    }
}

我想要附加属性的按钮是由ItemsControl生成的:

 <ItemsControl x:Name="frameSelector" ItemsSource="{Binding Path=FrameSelectors}" Grid.Column="2" Grid.RowSpan="2" Width="150" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch">
 <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
         <UniformGrid Columns="1"/>
     </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
 <ItemsControl.ItemTemplate>
     <DataTemplate>
         <Button Content="{Binding Path=DisplayName}" Command="{Binding Path=ButtonCommand}" CommandParameter="{Binding Path=CommandParameter}"
                 Style="{StaticResource ResourceKey=FrameSelectorStyle}" FontSize="28" local:FormSelectorExtension.IsSelected="{Binding Path=Selected}"/>
     </DataTemplate>
 </ItemsControl.ItemTemplate>

现在的问题是&#34;选择&#34;我视图模型的属性到attatched属性。如果我使用静态值&#34; SelectionChanged&#34; attatched属性的eventhandler被正确调用。

一旦我加入绑定,就没有任何反应。 (初始值设置正确但更改无法识别。)

静态值

local:FormSelectorExtension.IsSelected="True"

结合

local:FormSelectorExtension.IsSelected="{Binding Path=Selected}"

我更新视图模型的代码:

private void ActivateFrame(FormsFrame selectedFrame)
{
    ActiveFrame = selectedFrame;    

    foreach(var selector in FrameSelectors)
        selector.Selected = selector.DisplayName == selectedFrame.DisplayName;

    OnPropertyChanged("FrameSelectors");
    OnPropertyChanged("ActiveFrame");
}

我的ViewModel:

public class ButtonData
{
    public String DisplayName { get; set; }
    public bool Selected { get; set; }

    public ICommand ButtonCommand { get; set; }
    public object CommandParameter { get; set; }
}

1 个答案:

答案 0 :(得分:1)

假设FrameSelectors是public String SplitString(string inputString, int sectionLength, char splitChar) { if (inputString.Length <= sectionLength || sectionLength < 1) return inputString; string returnString = ""; int subStart; int end = inputString.Length; for (subStart = 0 ; (subStart + sectionLength) < end; subStart += sectionLength) { returnString = returnString + inputString.Substring(subStart, sectionLength) + splitChar; } return returnString + inputString.Substring(subStart, end - subStart); } 个实例的集合,您还必须在ButtonData类上实现ButtonData

INotifyPropertyChanged

注意,您的FormSelectorExtension类不需要从DependencyObject派生。这仅适用于常规依赖项属性,但不适用于附加属性。