Attached property binding proxy

风流意气都作罢 提交于 2019-12-12 02:55:36

问题


I have this binding in xaml:

<Popup IsOpen="{Binding Path=(local:ListViewBehavior.IsColumnHeaderClicked),
    RelativeSource={RelativeSource FindAncestor,  AncestorType=GridViewColumnHeader}}" ...

Popup is located inside GridViewColumn.Header.

ListViewBehavior.IsColumnHeaderClicked is a simple bool attached property:

public static bool GetIsColumnHeaderClicked(DependencyObject obj) => (bool)obj.GetValue(IsColumnHeaderClickedProperty);
public static void SetIsColumnHeaderClicked(DependencyObject obj, bool value) => obj.SetValue(IsColumnHeaderClickedProperty, value);
public static readonly DependencyProperty IsColumnHeaderClickedProperty =
    DependencyProperty.RegisterAttached("IsColumnHeaderClicked", typeof(bool), typeof(ListViewBehavior), new PropertyMetadata(false));

Its value is set by some code behind:

void listView_ColumnClicked(object sender, RoutedEventArgs e)
{
    var column = (DependencyObject)e.OriginalSource;
    SetIsColumnHeaderClicked(column, !GetIsColumnHeaderClicked(column)); // toggle
}

Idea is to show popup when GridViewColumnHeader is clicked. Everything so far is working.

Question: how bind this attached property to IsPopupOpen property in my ViewModel?

I was thinking about some kind of proxy which is used to bind to attached property and to ViewModel property at same time. Or perhaps I should bind to IsOpen again? Bind twice? Is this even possible?

来源:https://stackoverflow.com/questions/36041383/attached-property-binding-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!