2013-10-12 9 views
0

사용자가 두 값을 입력 할 때 두 가지 형식 유형 중 하나를 선택할 수있게하려는 경우 xaml에서 DataTrigger를 사용하여 두 가지 중 하나를 선택하려고합니다. 사용자가 선택한 형식에 따라이 값을 입력 할 수있는 별도의 컨트롤 유형.WPF - 자식 UIElements의 DataContext를 방해하지 않고 DataTrigger를 사용하는 방법

나는보기 및보기 모델이 있습니다. 뷰 모델은 다음과 같은 속성이 있습니다 DataTrigger이의 발동하기위한 "FormatA"또는 "FormatB"같은에 DoubleFormat 속성을 설정합니다

public class MyViewModel 
{ 
    public string DoubleFormat {get;set;} 
    public double X {get;set;} 
    public double Y {get;set;} 
    public double Z {get;set;} 
} 

응용 프로그램.

내보기의 XAML은 다음과 같습니다

<DataTemplate x:Key="FormatAEditTemplate" DataType="common:MyViewModel"> 
    <util:FormatAEditorControl Value="{Binding X}"/> 
    </DataTemplate> 

    <DataTemplate x:Key="FormatBEditTemplate" DataType="common:MyViewModel"> 
    <wpftk:DecimalUpDown Value="{Binding X}" /> 
    </DataTemplate> 

    <DataTemplate x:Key="MyEditTemplate" DataType="common:MyViewModel"> 
    <ContentControl x:Name="MyDoubleInputControl" /> 
    <DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding DoubleFormat}" Value="FormatA"> 
      <Setter TargetName="MyDoubleInputControl" Property="ContentTemplate" Value="  
      {StaticResource FormatAEditTemplate}" /> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding DoubleFormat}" Value="FormatB"> 
      <Setter TargetName="MyDoubleInputControl" Property="ContentTemplate" Value=" 
      {StaticResource FormatBEditTemplate}" /> 
     </DataTrigger> 
    </DataTemplate.Triggers> 
    </DataTemplate> 

그래서 트리거에 나는 기본적으로 렌더링 상단에있는 두 개의 템플릿 값 중 하나를 동일하기은 ContentControl의 ContentTemplate 속성을 설정하기 위해 노력하고있어 중 하나 util : FormatAEditorControl 또는 wpftk : DecimalUpDown - 사용자가 viewmodel의 속성 X에 대한 double 값을 편집합니다.

트리거 는 작업을 수행하고 정확한 컨트롤 타입 그러나 입력의 바인딩은 하지 작업을 수행 자체를 제어하는 ​​렌더링이다. 컨트롤이 렌더링되지만 double 속성의 "0.0"기본값이 없거나 U/I의 속성을 업데이트해도 viewmodel 속성 값에 영향을주지 않습니다. 또한 WPF 출력 로깅을 켜면 출력 창에 다음 출력이 표시됩니다.

System.Windows.Data 정보 : 41 : BindingExpression 경로 오류 : 'X'속성이 'object' item이 null입니다. 이는 데이터 제공 업체가 아직 데이터를 생성하지 않았기 때문에 발생할 수 있습니다. BindingExpression : Path = X; DataItem = null; 대상 요소는 'DecimalUpDown'(Name = 'FooBar')입니다. 대상 속성이 '값'(유형 'Nullable 1') System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=X; DataItem=null; target element is 'DecimalUpDown' (Name='FooBar'); target property is 'Value' (type 'Nullable 1') System.Windows.Data 정보 : 21 : BindingExpression이 null 데이터 항목에서 값을 검색 할 수 없습니다. 바인딩이 분리되거나 아무 값도없는 Nullable 형식에 바인딩 될 때이 오류가 발생할 수 있습니다. BindingExpression : Path = X; DataItem = null; 대상 요소는 'DecimalUpDown'(Name = 'FooBar')입니다. 대상 속성이 '값'(유형 'Nullable 1') System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=X; DataItem=null; target element is 'DecimalUpDown' (Name='FooBar'); target property is 'Value' (type 'Nullable 1')

... 본질적으로 똑같은 상태로 보이는 몇 가지 다른 줄과 함께.

데이터 트리거 대신 xaml에 "하드 코드"하는 두 가지 컨트롤 유형 중 하나를 선택하면 데이터 바인딩이 제대로 작동하므로 DataTrigger의 Binding 속성을 설정하는 것이 방해가 될 수 있습니다. 트리거에 의해 선택되는 "자식"요소의 DataContext를 사용합니다.

그렇다면 기본적으로이 전체보기의 모든 마지막 항목을 MyViewModel을 DataContext로 사용하고 싶기 때문에 DataTrigger에 두 가지 입력 컨트롤 유형 중 하나를 선택하지 않아도됩니다. DataContexts? 아니면이 시나리오를 근본적으로 오해하고 있다면 누군가 내게 뼈를 던질 수도 있습니다. 가 {바인딩 RelativeSource = {RelativeSource 모드 = FindAncestor, AncestorType = {x :는 : 유형은 ContentControl}} 경로 = DataContext.X} 하지만이처럼 보인다

은혜로운 동료는 다음과 같이 결합함으로써이 문제를 해결하기 위해 관리 상당히 단호한 해결 방법.

감사합니다.

답변

1

당신은 ContentControl을 사용하고 있으므로, 템플릿 기반 콘텐츠는 본래 Content 속성을 참조합니다. 위에서 쓴 것에서 볼 때 그 속성은 설정되지 않았으므로 바인딩 오류가 발생했습니다. 정확히

<DataTemplate x:Key="MyEditTemplate" DataType="common:MyViewModel"> 
    <ContentControl x:Name="MyDoubleInputControl" Content="{Binding}" /> 

    ... 

</DataTemplate> 
+0

:

는 같은 ContentControl을 설정하려고합니다. 감사. 나는 또한 이것을 발견했다. http://stackoverflow.com/questions/4480388/frameworkelements-datacontext-property-does-not-inherit-down-the-element-tree. 이 특별한 쟁점은 현재의 경험 수준에서의 불쾌한 잡동사니처럼 느껴지지만, 그것은 단순하지 않을 수도 있습니다. –

관련 문제