WPF 연결 속성 데이터 바인딩
속성이 첨부된 바인딩을 사용하려고 합니다.하지만 작동할 수 없습니다.
public class Attached
{
public static DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("TestProperty", typeof(bool), typeof(Attached),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Inherits));
public static bool GetTest(DependencyObject obj)
{
return (bool)obj.GetValue(TestProperty);
}
public static void SetTest(DependencyObject obj, bool value)
{
obj.SetValue(TestProperty, value);
}
}
XAML 코드:
<Window ...>
<StackPanel local:Attached.Test="true" x:Name="f">
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay, RelativeSource={RelativeSource Self}}" />
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay}" />
</StackPanel>
</Window>
바인딩 오류:
System.Windows.Data Error: 40 : BindingExpression path error: '(local:Attached.Test)' property not found on 'object' ''StackPanel' (Name='f')'. BindingExpression:Path=(local:Attached.Test); DataItem='StackPanel' (Name='f'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
믿거나 말거나, 그냥 추가하세요.Path=연결된 속성에 바인딩할 때 괄호를 사용합니다.
IsChecked="{Binding Path=(local:Attached.Test), Mode=TwoWay, RelativeSource={RelativeSource Self}}"
추가로, 당신의 전화는RegisterAttached는 "TestProperty"가 아닌 "Test"를 속성 이름으로 전달해야 합니다.
켄트의 답변에 대한 코멘트로 올리고 싶었지만, 제가 그렇게 할 충분한 담당자가 없기 때문에...WPF 4.5 기준으로 지적하고 싶었을 뿐입니다.Path=더 이상 필요하지 않습니다.그러나 첨부된 속성 이름은 여전히 괄호로 묶어야 합니다.
브래킷을 설치하면 작동합니다.부모의 자동화 ID 바인딩을 수행해야 했습니다.contentcontrol아주textblock에datatemplate자동화 ID는 연결된 속성입니다.
나는 자산을 괄호 안에 넣고 바인딩 작업을 했습니다.
AutomationProperties.AutomationId="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContentControl},Path=(AutomationProperties.AutomationId)}"
언급URL : https://stackoverflow.com/questions/5832208/wpf-attached-property-data-binding
'programing' 카테고리의 다른 글
| Kestrel이란 무엇입니까(vs IIS / Express) (0) | 2023.05.07 |
|---|---|
| SQL Server에서 중복된 행을 삭제하는 방법은 무엇입니까? (0) | 2023.05.07 |
| 페이지 앵커에 해시태그를 사용한 Angular2 라우팅 (0) | 2023.05.07 |
| 분기의 내용을 새 로컬 분기로 복사하려면 어떻게 해야 합니까? (0) | 2023.05.07 |
| Postgre에서 외부 키가 있는 행 삭제SQL (0) | 2023.05.07 |