2013-04-01 48 views
0

我想能夠>通過外部ComboBox一個DataGrid的細胞來設置當前選擇的行的值。數據網格值綁定到外部組合框

我是在二傳手的一部分,工作正常,但不能使ComboBox選定值匹配網格值代碼...看來我缺少的映射。

這是我有:

1- DataGrid綁定到一個ObservableCollection<Object>

<DataGrid ItemsSource="{Binding}" 
      SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=2}, 
      Path=SelectedCounterparty, Mode=TwoWay}"> 

2- ObservableCollection<Object>具有被選擇,我應該結合到ComboBox一個屬性(即,組合框項應該採取屬性值):

public CurrenciesEnum Ccy 
{ 
    get { return this._ccy; } 
    set 
    { 
     if (value != this._ccy) 
     { 
      this._ccy = value; 
      NotifyPropertyChanged("Ccy"); 
     } 
    } 
} 

3-組合框源是一個枚舉:

public enum CurrenciesEnum { USD, JPY, HKD, EUR, AUD, NZD }; 

4- ComboBox的當前映射:

<ObjectDataProvider x:Key="Currencies" MethodName="GetNames" ObjectType="{x:Type System:Enum}"> 
    <ObjectDataProvider.MethodParameters> 
     <x:Type TypeName="ConfigManager:CurrenciesEnum" /> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 

<ComboBox ItemsSource="{Binding Source={StaticResource Currencies}}" SelectedItem="{Binding Ccy, Mode=TwoWay}"/> 

什麼工作:能夠通過組合框設置當前所選項目的「CCY」屬性在網格中。

什麼並不:組合框選擇的項目不是變化的線路時匹配在網格當前選擇的項目(和默認爲USD或先前選擇的值),換句話說似乎並不正確地綁定。任何想法如何解決這個問題

+0

爲什麼' SelectedValuePath =「{Binding Ccy}」' - 只需嘗試'SelectedItem' – NSGaga

+0

你有'_spc1Ccy' d _ccy'支持'相同的屬性 – NSGaga

+0

更正了這兩個備註。就像我在同一時間測試一樣,只是錯字。這個問題仍然是相同的,雖然 – goul

回答

0

終於找到了問題。

這是因爲我在那裏綁定到枚舉本身的事實,一個ComboBox SelectedItems還給一個字符串(CSharp Corner: Binding an Enum to a ComboBox)。

因此,要解決我已經定義了一個轉換器,並在使用它的問題,我結合:

轉換器:

[ValueConversion(typeof(string), typeof(Object))] 
public class StringToCurrencyEnumConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value!=null) 
     { 
      CurrenciesEnum enumValue = (CurrenciesEnum)value; 
      return enumValue; 
     } 
     return null; 
    } 

    public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) 
    { 
     if (value != null) 
     { 
      string temp = ((CurrenciesEnum) value).ToString(); 
      return temp; 
     } 
     return null; 
    } 
} 

更新綁定:

<Grid.Resources> 
    <local:StringToCurrencyEnumConverter x:Key="StringToCcyEnum" /> 
</Grid.Resources> 

<ComboBox 
    ItemsSource="{Binding Source={StaticResource Currencies}}" 
    SelectedValue="{Binding Spc1Ccy, Mode=TwoWay, Converter={StaticResource StringToCcyEnum}}"/> 
0

讓我們假設你已經綁定了ObservalbeCollection<MyDataGridItem> MyDataGridItems到datagrid ItemsSource屬性。

view model中定義一個屬性,以按如下方式綁定數據網格的SeletedItem

private MyDataGridItem SelectedDataGridRow; 

public MyDataGridItem SelectedDataGridRow 
{ 
    get 
    { 
     return selectedDataGridRow; 
    } 
    set 
    { 
     selectedDataGridRow= value; 
     NotifyPropertyChanged("SelectedDataGridRow"); 
    } 
} 

比方說,你是要去綁定到DataGrid列的屬性是MyColumn(MyColumn爲MyDataGridItem類的屬性)

然後在CCY財產的setter方法,設置MyColumn屬性如下。

public CurrenciesEnum Ccy 
{ 
    get { return this._ccy; } 
    set 
    { 
     if (value != this._ccy) 
     { 
      this._ccy = value; 

      //This is the code you need to implement 

       this.MyDataGridItems 
        .Where(item=> item== this.SelectedDataGridRow) 
        .First() 
        .MyColumn = value; 

      NotifyPropertyChanged("Ccy"); 
     } 
    } 
} 
+0

非常感謝。實際上找到了另一種方式,參見。我對這個問題的回答 – goul