2015-06-19 99 views
2

我有一個應用程序遵循MVVM模式,它必須序列化XML文件。由於一些XML屬性的嵌套,我創建嵌套類我的模型裏面,像這樣:模型中嵌套類屬性的ViewModel屬性

public class OPER_FILE 
{ 
    public UNB unb { get; set; } 
    public OPER oper { get; set; } //nested class, level 1 

    public OPER_FILE() 
    { 
     unb = new UNB(); 
     oper = new OPER(); 
    } 
} 

public class OPER 
{ 
    public UNH unh { get; set; } //these are all nested classes, level 2 
    public UVT uvt { get; set; } 
    public VIN vin { get; set; } 

    public OPER() 
    { 
     unh = new UNH(); 
     uvt = new UVT(); 
    } 
} 

#region "nested classes" 
public class UNB 
{ 
    public string unb { get; set; } 
    public string unb_2 { get; set; } 
} 

public class UNH 
{ 
    public string unh { get; set; } 
    public string unh_2 { get; set; } 
} 

public class UVT 
{ 
    public string uvt { get; set; } 
    public string uvt_1 { get; set; } 
    public string uvt_2 { get; set; } 
} 

public class VIN 
{ 
    public string vin { get; set; } 
    public string vin_1 { get; set; } 
    public string vin_2 { get; set; } 
    public string vin_3 { get; set; } 
    public string vin_4 { get; set; } 
} 
#endregion 

嵌套類的屬性都是字符串,因爲這簡化了對於現在的XML序列化(I」 m仍處於概念階段)。

在我相應的ViewModel中,我只是在模型內部爲嵌套類創建了一個屬性,所以我可以通過引用ViewModel中的嵌套類來訪問所有嵌套屬性。

public class OPERViewModel : IViewModelBase 
{ 
    private OPER_FILE Model; 

    public UNB unb 
    { 
     get 
     { return Model.unb;} 
     set 
     { Model.unb = value; } 
    } 

    public OPER oper 
    { 
     get 
     { return Model.oper; } //this is the tricky part, by now I'm just referring to the nested class as a property of the model 
     set 
     { Model.oper = value; } 
    } 

    public OPERViewModel() 
    { Model = new OPER_FILE(); } 
} 

的問題是,但是,我想要顯示的某些屬性不是字符串,但使用的UI複選框爲布爾值。

說我要爲布爾(其中視圖模型應該管理從string轉換爲在Model.oper.vin.vin_1自有物業反射吸氣bool)顯示Model.oper.vin.vin_1,我會怎麼做呢?

我是否真的必須實現嵌套類的每個嵌套屬性作爲ViewModel的自己的屬性(如下所述)以獲得對返回到UI的方式的控制權?

//ViewModel-implementation with type-conversion of a property from a nested class of the model 
//this would get bind to the UI instead of Model.oper.vin.vin_1 
public bool vin_1  
{ 
    get 
    { 
     if (Model.oper.vin.vin_1 == "1") 
     { return true; } 
     else 
     { return false; } 
    } 
    set 
    { 
     if (value) 
     { Model.oper.vin.vin_1 = "1"; } 
     else 
     { Model.oper.vin.vin_1 = "0"; } 
    } 
} 

我希望有一個更好的解決方案有...


編輯:

什麼我忘了之前提到,那裏是不僅string S作顯示爲boolean s,也DateTime-應顯示爲DatePicker的值 - 控制,我希望作爲NumberPicker的整數值和等等。和float - 值,特殊日期格式和逗號 -

的XML文件,而另一方面,將通過接口與一些非常固定的規則,我需要匹配,兩個integer這樣一個動態的前導零消耗而不是小數點分隔符。因此,堅持使用string-要在序列化對象內的值是維護對xml文件中的值進行實際解析的方式的一種好方法。

我會嘗試嘗試使用一些不同的轉換器,因爲@BrandlyDOTNet推薦,但仍然對如何以其他方式解決這個問題感到好奇。

回答

2

有一個不同解決方案在那裏,即可以使用轉換器來定義你的字符串和布爾之間的轉換。

喜歡的東西:

public class StringToBoolConverter : IValueConverter 
{ 
    public object Convert(...) 
    { 
     return value.ToString() != "0"; 
    } 

    public object ConvertBack(...) 
    { 
     bool boolVal = (bool)value; 
     return boolVal ? "1" : "0"; 
    } 
} 

用法:

<CheckBox IsChecked={Binding SomeProp, Converter={StaticResource StringToBoolConverter}"/> 

但是,爲了回答你的更深層次的問題,不,該框架將不只是轉換爲字符串 「1」 變成了布爾。此外,你可以強制鍵入你正在被序列化的對象,所以這些都不是必要的

+0

*強調打字*你的意思是將所有這些'字符串'改爲實際有用的數據類型,更好地匹配它們的值。隨着項目的進展,我可以並且非常期待。但是我必須接受一個接口的特殊需求,這個接口是用來使用xml文件的,並且有一些我必須匹配的嚴格固定的格式規則。由於快速應用程序開發是一天的順序,我希望現在可以堅持使用字符串變量,並找到一些可以減少寫入代碼量的東西。 所以我做了,謝謝你,我會看看這個'Converter'的東西。 – M463