2011-12-08 24 views
3

我對MVVM相當陌生,請耐心等待。我具備擁有如此實施的公共屬性視圖模型類:綁定到集合中的單個元素

public List<float> Length 
{ 
    get; 
    set; 
} 

在我的XAML的看法,我有幾個文本框,每個人在這個長列表綁定到特定的元素:

<TextBox Text="{Binding Length[0], Converter=DimensionConverter}" /> 
    <TextBox Text="{Binding Length[2], Converter=DimensionConverter}" /> 
    <TextBox Text="{Binding Length[4], Converter=DimensionConverter}" /> 

DimensionConverter是一個IValueConverter派生類,它將值的形式設置爲一個尺寸(即480.0英寸在屏幕上的文本框中變爲40'0「),然後再返回(即對於字符串需要35'0」產量420.0英寸)

我的問題:我需要能夠驗證List中的每個值,因爲它在關聯的TextBox中被更改。對於某些人,我可能需要修改列表中的其他值,具體取決於輸入的值(即更改浮點數在長度[0]將更改長度[4]處的值並更新屏幕)。

有什麼辦法重新工作的財產,以允許索引?或者,我是否需要爲列表中的每個項目創建單獨的屬性(這實際上使列表成爲不必要的)?基本上,因爲我已經有了float的集合,所以我希望能夠編寫MVVM代碼來驗證每個項目被修改時的狀態。

想法? (並且,提前致謝)

回答

1

您可以使用ObservableCollection<float>而不是List<float>,並處理CollectionChanged事件以檢測用戶何時更改值。

+0

據我瞭解,他不能通過項目索引綁定到特定列表項,我錯過了什麼? – sll

+0

這不是我所理解的。顯然,OP要驗證用戶輸入,因此他需要檢測用戶何時更改值 –

+0

是的,看起來我完全誤解了這個問題 – sll

1

豈不是這樣的:

<ItemsControl ItemsSource="{Binding Length}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Mode=TwoWay, Converter=DimensionConverter}" /> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

接近你想要什麼?
它將顯示整個列表,並允許用戶修改值,只要您的IValueConverter實現ConvertBack,它將直接返回列表。

然後做托馬斯說來驗證,或實施ObservableLinkedList

你現在在做什麼看起來髒髒的已經和它的代碼幾乎沒有幾行..

+0

我遇到的問題是視圖的性質I'試圖創造。基本上這個視圖是建築物佈局的,所以長度項目是牆壁的長度,各種配置(矩形,L形,T形等)。佈局顯示爲圖表,TextBox控件用於輸入單個測量。我已經有一個牆的列表,所以我希望只是在綁定中訪問它,而不是爲集合中的每個項目創建一個樣板方法。 –

1

它將如果你能成爲偉大有一個類實現INotifyPropertyChanged具有提供的屬性列表的長度是恆定的。如果你想驗證與MVVM您的文本輸入,然後創建可以在您的視圖模型在你的視圖模型

public class FloatClass : INotifyPropertyChanged 
{ 
    private ICollection parentList; 

    public FloatClass(float initValue, ICollection pList) { 
    parentList = pList; 
    this.Value = initValue; 
    } 

    private float value; 

    public float Value { 
    get { return this.value; } 
    set { 
     if (!Equals(value, this.Value)) { 
     this.value = value; 
     this.RaiseOnPropertyChanged("Value"); 
     } 
    } 
    } 

    private void RaiseOnPropertyChanged(string propName) { 
    var eh = this.PropertyChanged; 
    if (eh != null) { 
     eh(this, new PropertyChangedEventArgs(propName)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

憂色您可以使用該模型這樣

public class FloatClassViewmModel : INotifyPropertyChanged 
{ 
    public FloatClassViewmModel() { 
    this.FloatClassCollection = new ObservableCollection<FloatClass>(); 
    foreach (var floatValue in new[]{0f,1f,2f,3f}) { 
     this.FloatClassCollection.Add(new FloatClass(floatValue, this.FloatClassCollection)); 
    } 
    } 

    private ObservableCollection<FloatClass> floatClassCollection; 

    public ObservableCollection<FloatClass> FloatClassCollection { 
    get { return this.floatClassCollection; } 
    set { 
     if (!Equals(value, this.FloatClassCollection)) { 
     this.floatClassCollection = value; 
     this.RaiseOnPropertyChanged("FloatClassCollection"); 
     } 
    } 
    } 

    private void RaiseOnPropertyChanged(string propName) { 
    var eh = this.PropertyChanged; 
    if (eh != null) { 
     eh(this, new PropertyChangedEventArgs(propName)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

這裏是一個模型

+0

僅供參考:提供Length屬性的ViewModel類實現INotifyPropertyChanged –

1

xaml示例

<ItemsControl ItemsSource="{Binding Path=FloatClassViewmModel.FloatClassCollection}"> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <TextBox Text="{Binding Value, Mode=TwoWay, Converter=DimensionConverter}" /> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

希望這有助於

+0

我曾考慮過這種方法。我猶豫不決,因爲它似乎「沉重」給我,但我對MVVM相當陌生,所以從我的C++時代開始,可能是剩餘的。 –

相關問題