4
Visibility 속성으로 설정된 내 ViewModel의 속성을 사용하여 확장기에서 토글 단추를 동적으로 숨기려고합니다. 적어도 그건 내 생각이다. 내 xaml에서 Toggle Button의 전체 템플릿을 수행 할 필요없이 ToggleButton 컨트롤에서 해당 설정을 변경하는 방법이 있습니까?바인딩을 통해 WPF 확장기에서 ToggleButton 숨기기
Visibility 속성으로 설정된 내 ViewModel의 속성을 사용하여 확장기에서 토글 단추를 동적으로 숨기려고합니다. 적어도 그건 내 생각이다. 내 xaml에서 Toggle Button의 전체 템플릿을 수행 할 필요없이 ToggleButton 컨트롤에서 해당 설정을 변경하는 방법이 있습니까?바인딩을 통해 WPF 확장기에서 ToggleButton 숨기기
당신은 Expander
에 대한 Loaded
이벤트를 사용하여 이벤트 처리기에서 Binding
을 설정하거나 코드없이 재사용 방법을 원하는 경우 연결된 동작을 사용할 수 있습니다 뒤에 수 있습니다.
첨부 된 동작은 Template
내부의 ToggleButton
을 발견하고 연결된 속성 ToggleButtonVisibility
에 Binding
을 설정합니다.
이
<Expander Name="expander"
behaviors:ExpanderBehavior.BindToggleButtonVisibility="True"
behaviors:ExpanderBehavior.ToggleButtonVisibility="{Binding YourVisibilityProperty}"
.../>
ExpanderBehavior
public class ExpanderBehavior
{
public static DependencyProperty BindToggleButtonVisibilityProperty =
DependencyProperty.RegisterAttached("BindToggleButtonVisibility",
typeof(bool),
typeof(ExpanderBehavior),
new PropertyMetadata(false, OnBindToggleButtonVisibilityChanged));
public static bool GetBindToggleButtonVisibility(Expander expander)
{
return (bool)expander.GetValue(BindToggleButtonVisibilityProperty);
}
public static void SetBindToggleButtonVisibility(Expander expander, bool value)
{
expander.SetValue(BindToggleButtonVisibilityProperty, value);
}
private static void OnBindToggleButtonVisibilityChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
Expander expander = target as Expander;
if (expander.IsLoaded == true)
{
BindToggleButtonVisibility(expander);
}
else
{
RoutedEventHandler loadedEventHandler = null;
loadedEventHandler = new RoutedEventHandler(delegate
{
BindToggleButtonVisibility(expander);
expander.Loaded -= loadedEventHandler;
});
expander.Loaded += loadedEventHandler;
}
}
private static void BindToggleButtonVisibility(Expander expander)
{
ToggleButton headerSite = expander.Template.FindName("HeaderSite", expander) as ToggleButton;
if (headerSite != null)
{
Binding visibilityBinding = new Binding
{
Source = expander,
Path = new PropertyPath(ToggleButtonVisibilityProperty)
};
headerSite.SetBinding(ToggleButton.VisibilityProperty, visibilityBinding);
}
}
#region ToggleButtonVisibilityProperty
public static DependencyProperty ToggleButtonVisibilityProperty =
DependencyProperty.RegisterAttached("ToggleButtonVisibility",
typeof(Visibility),
typeof(ExpanderBehavior),
new PropertyMetadata(Visibility.Visible));
public static Visibility GetToggleButtonVisibility(Expander expander)
{
return (Visibility)expander.GetValue(ToggleButtonVisibilityProperty);
}
public static void SetToggleButtonVisibility(Expander expander, Visibility value)
{
expander.SetValue(ToggleButtonVisibilityProperty, value);
}
#endregion // ToggleButtonVisibilityProperty
}
감사처럼 사용합니다. 이 일을 한 번 살펴 보겠습니다. –
re-templating 외에도 다른 옵션이 있습니다. 'Expander'를 서브 클래스 화하고'OnApplyTemplate'에서'ToggleButton'을 찾을 수 있습니다. 내 컴퓨터에 액세스 할 수 없으므로 예제를 제공 할 수 없지만 여기서 비슷한 것을 찾을 수 있습니다. http://stackoverflow.com/questions/5111756/in-wpf-how-to-add-an-eventhandler-for- a-frameworkelement-designed-in-template –