2011-10-15 2 views
0

저는 현재 기본적으로 실버 라이트 컨트롤을 확장하여 레이블과 물건을 추가하는 일반적인 컨트롤을 만들고 있습니다.하지만 바인딩을 얻는 데 어려움을 겪고 있습니다. 작업 ... 다음은 현재 작동하지 않는 사항의 예입니다커스텀 컨트롤과 바인딩에 문제가 있습니다.

UPDATE :

public class ComboboxField : Field 
{ 

    public string SelectedValuePath 
    { 
     get { return (string)this.GetValue(SelectedValuePathProperty); } 
     set { this.SetValue(SelectedValuePathProperty, value); } 
    } 
    public static readonly DependencyProperty SelectedValuePathProperty = 
    DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboboxField), new PropertyMetadata(string.Empty)); 


    public string DisplayMemberPath 
    { 
     get { return (string)this.GetValue(DisplayMemberPathProperty); } 
     set { this.SetValue(DisplayMemberPathProperty, value); } 
    } 
    public static readonly DependencyProperty DisplayMemberPathProperty = 
     DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(ComboboxField), new PropertyMetadata(string.Empty, null)); 



    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable)this.GetValue(ItemsSourceProperty); } 
     set { this.SetValue(ItemsSourceProperty, value); } 
    } 

    public static readonly DependencyProperty ItemsSourceProperty = 
    DependencyProperty.Register("ItemsSource", typeof(Object), typeof(ComboboxField), new PropertyMetadata(new List<object>())); 



    public object SelectedValue 
    { 
     get { return (object)GetValue(SelectedValueProperty); } 
     set { SetValue(SelectedValueProperty, value); } 
    } 

    public static readonly DependencyProperty SelectedValueProperty = 
     DependencyProperty.Register("SelectedValue", typeof(object), typeof(ComboboxField), new PropertyMetadata(null, (s, e) => { s.SetValue(Field.ValueProperty, SelectedValueProperty);})); 

    #region Ctors 
    public ComboboxField(FieldDescription fieldDescription, Form parentForm) : base(fieldDescription, parentForm) 
    { 
     this.Template = Application.Current.Resources["ComboBoxDefaultTemplate"] as ControlTemplate; 
    } 

    public ComboboxField() : base(new FieldDescription(Guid.NewGuid(), "ComboboxField1", FieldType.Collection), null) 
    { 
     this.Template = Application.Current.Resources["ComboBoxDefaultTemplate"] as ControlTemplate; 
    } 
    #endregion 

} 

컨트롤 템플릿 :

여기에 문제를 이해하는 데 도움이되는 몇 가지 더 상황이있다 :

<cc:ComboboxField Grid.Row="10" TitleStyle="{StaticResource labelStyle}" 
            Title="Countries" ItemsSource="{Binding Countries}" 
            SelectedValuePath="CountryId" DisplayMemberPath="CountryId" 
            SelectedValue="{Binding Path=SelectedCountry, Mode=TwoWay}" /> 

내가 잘못 여기서 뭐하는 거지 : 내가 그것을 사용하려고 해요 방법

<ControlTemplate x:Name="ComboBoxDefaultTemplate" TargetType="my:ComboboxField"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="{TemplateBinding LabelColumnWidth}" /> 
        <ColumnDefinition Width="{TemplateBinding FieldColumnWidth}" /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Grid.Row="0" Grid.Column="0" Style="{TemplateBinding TitleStyle}" Text="{TemplateBinding Title}"></TextBlock> 
       <TextBlock Text="*" Grid.Row="0" Grid.Column="0" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Required, Converter={StaticResource boolToVisibility}}" Width="5" /> 
       <TextBlock Grid.Row="1" Grid.Column="0" Style="{TemplateBinding SummaryStyle}" Text="{TemplateBinding Summary}" Grid.ColumnSpan="2"></TextBlock> 
<ComboBox Style="{TemplateBinding FieldStyle}" Grid.Row="0" Grid.Column="1" 
          ItemsSource="{TemplateBinding ItemsSource}" 
          SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedValue, Mode=TwoWay}" 
          SelectedValuePath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedValuePath, Mode=TwoWay}" 
          DisplayMemberPath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMemberPath, Mode=TwoWay}"></ComboBox> 
      </Grid> 
     </ControlTemplate> 

(무관 한 물건의 일부를 제거)?

+1

NotifyPropertyChanged 호출은 중복됩니다. 기본 스타일은 키없이 Themes/Generic.xaml 파일에 배치되어야합니다. 또한 응용 프로그램을 디버깅하는 동안 출력 창을 볼 수 있으며 모든 바인딩 오류가 표시됩니다. 응용 프로그램에 바인딩 오류가 표시되면 해당 정보를 질문에 추가 할 수 있습니다. – vorrtex

+0

@vorrtex : 필자는 항상 출력 창 팁 덕분에 제 바인딩에 문제가 있는지 파악할 수 있었는지 궁금했습니다. :) – Kassem

답변

1

종속성 속성에 대한 몇 가지 문제점을 즉시 확인할 수 있습니다.

CLR을 속성 게터가의 getValue 만 호출을 포함해야하며 세터 SetValue는 만 호출을 포함해야합니다

첫째, 난 당신의 코드를 위반 종속성 속성 작업을위한 황금 규칙을 가지고있다.

예를 들어, SelectedValuePath 속성은 다음과 같이해야합니다 : 그것은 바인딩, 애니메이션이나 스타일을 사용 종속성 속성의 값을 변경하고자하는 경우

public string SelectedValuePath 
{ 
    get { return (string)this.GetValue(SelectedValuePathProperty); } 
    set { this.SetValue(SelectedValuePathProperty, value); } 
} 

실버 라이트는 당신의 재산을 호출하지 않습니다. 세터 중 하나에 중단 점을 놓으면 예상대로 자주 중단되지 않습니다.

INotifyPropertyChanged을 구현할 필요가있는 클래스가 없으면 두 개의 종속성 속성을 함께 바인딩 할 수 있습니다. NotifyPropertyChanged 메서드가 제거 할 수 있어야하는 PropertyChanged 이벤트를 발생시키는 것으로 가정합니다. 당신이 SelectedValue 재산 후자 변경, 당신은 SelectedValue 종속성 속성에 PropertyChangedCallback을 추가해야 할 때마다의 값으로 Value 속성 집합을 원하는 경우에도

.

두 번째로 SelectedValuePathProperty, DisplayMemberPathPropertyItemsSourceProperty에 대한 종속성 속성 등록이 잘못되었습니다. ComboboxField 클래스가 소유자 인 경우 소유자 클래스는 모두 Field으로 지정됩니다.

마지막으로, 당신은 당신이 당신은 당신의 문제가 정확히 무엇을 우리에게 말하고하지 않을

일부 바인딩 작업 점점 힘든 시간을 보내고

것을 말한다. 코드가 무엇을 기대하고 실제로 무엇을하고 있습니까?Form, FieldFieldDescription 클래스 나 FieldType 열거 형이 없으므로 코드를 실행할 수 없으므로 더 이상 도움이되지 않습니다.

+0

분명히 도움이됩니다. 문제를 해결하고 작업을 완료 한 경우 피드백을 받겠습니다. 그렇지 않으면, 문제를 더 잘 설명하려고 노력할 것입니다. 아마 당신이 그것을 해결하는 데 도움이 될 수 있습니다. 귀하의 의견을 보내 주셔서 감사합니다. – Kassem

+0

지금 모두 좋다. 고마워. 그래서 내가 바로 잡아 보자, 내 사용자 정의 컨트롤을 구현하지 말아야 INotifyPropertyChanged, 맞죠? – Kassem

+0

맞아요, 사용자 지정 컨트롤이 INotifyPropertyChanged를 구현해서는 안됩니다. –

관련 문제