WPF

[WPF] Binding Error - HorizontalContentAlignment, VerticalContentAlignment

Chanhongv 2024. 9. 11. 14:15

여느때와 같이 WPF Binding 을 열심히 작업 중이 었는데, Binding Error 가 발생할게 없는데? Binding Error 가 발생한 적이 있다.

 

더보기

Systehttp://m.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

 

Systehttp://m.Windows.Data Error: 4 :
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''.
BindingExpression:Path=HorizontalContentAlignment; DataItem=null;
target element is 'ComboBoxItem' (Name='');
target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

내가 본것은 ComboBoxItem 과 ListBoxItem 에서 HorizontalContentAlignment, VerticalContentAlignment 관련 Binding Error 가 발생하는 것인데 아마도 ItemControl 의 Item 들이 들어간다면 발생할 수 있을 것 같다.

 

우선 위 바인딩 에러는 App.xaml Resource 에

            <Style TargetType="ComboBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
            </Style>

            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="VerticalContentAlignment" Value="Top" />
            </Style>

 

를 추가하여 글로벌 속성을 적용하면 바인딩 에러가 발생하지 않는다.

 

이런 잔버그들은 마소에서 하루 빨리 해결해주었으면 좋겠다.