2013-04-01 31 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