2010-07-08 17 views
1

這是一個難以解釋的問題,但我希望它有一個相當簡單的解決方案。帶有Observable Collection的XAML Listbox中的動態內容

我在XAML中編寫了一個ListBox控件,並且在網格中佈置的ListBox.ItemTemplate的大部分佈局在整個列表中都是相同的。然而,ListBox是一個「問題解析器」,它挑選客戶訂單列表中的異常情況,並要求用戶做出某些決定或更改數據,然後才允許它進入郵資系統,這將收取我們用於調度客戶訂單的費用。

問題類型在帶有三個設置的枚舉中進行了描述,對於三個問題狀態中的兩個,我需要問題的文本描述(每種問題的不同描述以及文本中包含的動態數據)以及一個按鈕,對於最後一個問題類型,我需要一個文本描述和一個單選按鈕列表。

是否有任何方法在XAML中包含一種switch語句,通過告訴它什麼Enum值是針對問題順序來決定將內容放入網格單元格中哪些內容需要變化?

回答

2
+0

這似乎工作。不幸的是,沒有構建完整的演示應用程序來測試它,我知道的是我的解決方案編譯。不過,這絕對是朝着正確方向邁出的一步。謝謝! – bert 2010-07-09 08:31:07

1

嘗試使用由Josh寫的開關轉換器來實現,應爲你工作:

SwitchConverter - A 「switch語句」 爲XAML - http://josheinstein.com/blog/index.php/2010/06/switchconverter-a-switch-statement-for-xaml/

編輯:

這是SwitchConverter的代碼,因爲[Josh's] [1]網站似乎已關閉 -

/// <summary> 
/// A converter that accepts <see cref="SwitchConverterCase"/>s and converts them to the 
/// Then property of the case. 
/// </summary> 
[ContentProperty("Cases")] 
public class SwitchConverter : IValueConverter 
{ 
    // Converter instances. 
    List<SwitchConverterCase> _cases; 

    #region Public Properties. 
    /// <summary> 
    /// Gets or sets an array of <see cref="SwitchConverterCase"/>s that this converter can use to produde values from. 
    /// </summary> 
    public List<SwitchConverterCase> Cases { get { return _cases; } set { _cases = value; } } 
    #endregion 
    #region Construction. 
    /// <summary> 
    /// Initializes a new instance of the <see cref="SwitchConverter"/> class. 
    /// </summary> 
    public SwitchConverter() 
    { 
     // Create the cases array. 
     _cases = new List<SwitchConverterCase>(); 
    } 
    #endregion 

    /// <summary> 
    /// Converts a value. 
    /// </summary> 
    /// <param name="value">The value produced by the binding source.</param> 
    /// <param name="targetType">The type of the binding target property.</param> 
    /// <param name="parameter">The converter parameter to use.</param> 
    /// <param name="culture">The culture to use in the converter.</param> 
    /// <returns> 
    /// A converted value. If the method returns null, the valid null value is used. 
    /// </returns> 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     // This will be the results of the operation. 
     object results = null; 

     // I'm only willing to convert SwitchConverterCases in this converter and no nulls! 
     if (value == null) throw new ArgumentNullException("value"); 

     // I need to find out if the case that matches this value actually exists in this converters cases collection. 
     if (_cases != null && _cases.Count > 0) 
      for (int i = 0; i < _cases.Count; i++) 
      { 
       // Get a reference to this case. 
       SwitchConverterCase targetCase = _cases[i]; 

       // Check to see if the value is the cases When parameter. 
       if (value == targetCase || value.ToString().ToUpper() == targetCase.When.ToString().ToUpper()) 
       { 
        // We've got what we want, the results can now be set to the Then property 
        // of the case we're on. 
        results = targetCase.Then; 

        // All done, get out of the loop. 
        break; 
       } 
      } 

     // return the results. 
     return results; 
    } 

    /// <summary> 
    /// Converts a value. 
    /// </summary> 
    /// <param name="value">The value that is produced by the binding target.</param> 
    /// <param name="targetType">The type to convert to.</param> 
    /// <param name="parameter">The converter parameter to use.</param> 
    /// <param name="culture">The culture to use in the converter.</param> 
    /// <returns> 
    /// A converted value. If the method returns null, the valid null value is used. 
    /// </returns> 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

/// <summary> 
/// Represents a case for a switch converter. 
/// </summary> 
[ContentProperty("Then")] 
public class SwitchConverterCase 
{ 
    // case instances. 
    string _when; 
    object _then; 

    #region Public Properties. 
    /// <summary> 
    /// Gets or sets the condition of the case. 
    /// </summary> 
    public string When { get { return _when; } set { _when = value; } } 
    /// <summary> 
    /// Gets or sets the results of this case when run through a <see cref="SwitchConverter"/> 
    /// </summary> 
    public object Then { get { return _then; } set { _then = value; } } 
    #endregion 
    #region Construction. 
    /// <summary> 
    /// Switches the converter. 
    /// </summary> 
    public SwitchConverterCase() 
    { 
    } 
    /// <summary> 
    /// Initializes a new instance of the <see cref="SwitchConverterCase"/> class. 
    /// </summary> 
    /// <param name="when">The condition of the case.</param> 
    /// <param name="then">The results of this case when run through a <see cref="SwitchConverter"/>.</param> 
    public SwitchConverterCase(string when, object then) 
    { 
     // Hook up the instances. 
     this._then = then; 
     this._when = when; 
    } 
    #endregion 

    /// <summary> 
    /// Returns a <see cref="System.String"/> that represents this instance. 
    /// </summary> 
    /// <returns> 
    /// A <see cref="System.String"/> that represents this instance. 
    /// </returns> 
    public override string ToString() 
    { 
     return string.Format("When={0}; Then={1}", When.ToString(), Then.ToString()); 
    } 
} 
+0

鏈接已死,但您可以訪問文章[here](http://web.archive.org/web/20120202000409/http://josheinstein.com/blog/index.php/2010/06/switchconverter-a- switch語句換XAML) – akjoshi 2013-11-13 11:34:59