2012-10-12 42 views
1

我有一個ComboBox withing一個GridView柱:修復不能結合的observalbe收集

... 
      <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Info test"> 
       <GridViewColumn Header="Number" Width="120"> 
        <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding Path=extensions}" Width="105" IsEditable="True" HorizontalAlignment="Center" Margin="0,0,0,0" BorderThickness="0"> 
          <ComboBox.Resources> 
           <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">15</sys:Double> 
          </ComboBox.Resources> 
         </ComboBox> 
        </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
... 

在後面的代碼,「擴展」是ObserverableCollection<string>即得到初始化100%並填充(在此是在類的構造函數中):

public partial class MyForm : Window 
{ 
    ... 
    public ObservableCollection<string> extensions; 
    ... 

    public MyForm() 
    { 
     ... 
     Initialize(); 
    } 

    private Initialize() 
    { 
     extensions = new ObservableCollection<string>(); 
     extensions.Add("x100"); 
     extensions.Add("x101"); 
    } 
} 

但是當應用程序在組合框出現時運行時,綁定永遠不會發生。需要哪些附加步驟才能使其完整/正確?

+0

你的擴展是公共財產嗎? –

+0

@MiklósBalogh是的。我宣佈它是公開的。我會更新我的帖子以澄清這一點。 Thank.s – kmarks2

回答

2

首先不要使用公共字段,而是使用屬性。據我所知,公共領域不適用於綁定。

public ObservableCollection<string> extensions {get; private set;} 

其次,可能combobox的datacontext未設置爲MyForm實例。 試試這個

<ComboBox ItemsSource="{Binding Path=extensions, RelativeSource={RelativeSource AncestorType={x:Type MyForm}}}" ... > 
+0

如果我添加了額外的代碼,我得到一個錯誤「Type MyForm was not found。」。 – kmarks2

+0

可能有前綴是什麼您的IDE生成以達到該類 –

+0

我沒有做太多的WPF,所以我可以問什麼樣的前綴我可能要找? – kmarks2