2010-08-26 76 views
0

我想要的屬性,我DataContextValidationRule綁定到一個屬性:基於this threadWPF,在有效性規則屬性從未

public class ReleaseValidationRule : ValidationRule 
{ 
    // I want to bind a value from my DataContext to this property: 
    public CheckboxViewModels ValidReleases { get; set; } 
    ... 
} 

,我創建了CheckboxViewModels類只是充當一個包裝爲List<CheckboxViewModel>,以便該列表可以是DependencyProperty,以便我可以綁定到它。但是,在我的ValidationRuleValidate方法中,ValidReleases列表始終爲空。這是我的XAML:

<TextBox> 
    <TextBox.Text> 
     <Binding Path="Release" UpdateSourceTrigger="PropertyChanged"> 
      <Binding.ValidationRules> 
       <local:ReleaseValidationRule> 
        <local:ReleaseValidationRule.ValidReleases> 
         <local:CheckboxViewModels List="{Binding Path=Releases, 
          Converter={StaticResource debugConverter}}"/> 
        </local:ReleaseValidationRule.ValidReleases> 
       </local:ReleaseValidationRule> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

我知道Releases財產(就是我結合的CheckboxViewModelsList物業)擁有的內容,因爲我有一個TreeView只是TextBox顯示的Releases上面的內容。我在CheckboxViewModels.List綁定上的轉換器什麼都不做,它只是一個我可以設置斷點的地方。有趣的是,轉換器斷點永遠不會被擊中。就好像整個行<local:CheckboxViewModels List="{Binding Path=Releases, Converter={StaticResource debugConverter}}"/>從未得到執行,因此我的ValidationRule中的ValidReleases屬性永遠不會被設置。這是怎麼回事?

編輯:這裏的CheckboxViewModels樣子:

public class CheckboxViewModels : DependencyObject, IList<CheckboxViewModel>, 
    IEnumerable<CheckboxViewModel> 
{ 
    ...members necessary to implement IList, IEnumerable... 

    public static readonly DependencyProperty ListProperty = 
     DependencyProperty.Register(
      "List", 
      typeof(List<CheckboxViewModel>), 
      typeof(CheckboxViewModels), 
      new PropertyMetadata(new List<CheckboxViewModel>()) 
     ); 

    public List<CheckboxViewModel> List 
    { 
     get { return (List<CheckboxViewModel>)GetValue(ListProperty); } 
     set { SetValue(ListProperty, value); } 
    } 
} 

回答

0

我停止使用驗證規則,而是遵循this tutorial about implementing IDataErrorInfo。那是我的類實例在我進入this[string]的時候完全初始化以找出錯誤信息。我一直試圖傳入的數據只是來自同一實例上另一個屬性的數據。也就是說,我想使用來自Property2的一些數據來驗證Property1。隨着IDataErrorInfo,當我確認Property1並在必要時組裝的錯誤信息,我有機會獲得Property2根據需要,而不必通過任何東西。

0

聞起來像一個丟失的屬性更改通知的地方。很難從中看出CheckboxModel的意義,但是如果它是ObservableCollection,那麼您將在沒有任何額外工作的情況下更改屬性。這有意義嗎?

HTH,
Berryl

+0

我更新了我的問題,包括'CheckboxViewModels'代碼。我不確定我應該聯繫哪些事件。 – 2010-08-26 18:52:01

+0

我沒有看到一個要求使列表成爲DP,因爲它看起來像綁定源而不是目標。我現在要添加一個新的屬性ObservableCollection ,然後嘗試用它重新解決這個問題。你可以把它稱之爲像Releases_NEW那樣聰明,併發布任何綁定給你帶來麻煩。如果DP需要變得清晰,我就會看到一條嘗試並使之發揮作用的途徑,而且您仍然有一個起點。 – Berryl 2010-08-26 23:33:47

0

好吧,現在我只是覺得很傻。我有一個CheckboxViewModels的構造函數,它設置List = new List<CheckboxViewModel>()。我認爲這在某種程度上重置了某些東西?我刪除了該構造函數,並且List的初始值僅在方法中設置爲DependencyPropertynew PropertyMetadata(new List<CheckboxViewModel>())ValidReleases現在填充與下面的XAML預期:

<TextBox> 
    <TextBox.Text> 
     <Binding Path="Release" UpdateSourceTrigger="PropertyChanged"> 
      <Binding.ValidationRules> 
       <local:ReleaseValidationRule ValidatesOnTargetUpdated="True"> 
        <local:ReleaseValidationRule.ValidReleases> 
         <local:CheckboxViewModels 
          List="{Binding Path=Releases, Mode=OneWay}"/> 
        </local:ReleaseValidationRule.ValidReleases> 
       </local:ReleaseValidationRule> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

編輯:沒有那麼愚蠢畢竟:沒有一個構造函數CheckboxViewModels導致使用相同的列表類型CheckboxViewModels兩個依賴屬性,所以我不得不發佈以及Releases屬性和其他屬性中的其他數據。將構造函數添加回CheckboxViewModels導致ValidReleases不再有任何項目。我認爲我必須正確地約束某些東西。