我的 UserControl 的 XAML 名稱為“Clinical_Protocol”:
<Grid>
<DataGrid Name="ClinicalProtocolDataGrid"
ItemsSource="{Binding DataGridItems, ElementName=Clinical_Protocol}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Structure ID"
ItemsSource="{Binding ComboBoxItems, ElementName=Clinical_Protocol, Mode=TwoWay}"
SelectedItemBinding="{Binding SelectedStructureId, Mode=TwoWay}"
/>
<DataGridTextColumn Header="RT ROI Type Code"
Binding="{Binding RtRoiInterpretedTypeCode}"/>
<DataGridTextColumn Header="RT ROI Type Description"
Binding="{Binding RtRoiInterpretedTypeDescription}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
ItemsSource
后面代碼中DataGrid的依賴屬性:
internal ObservableCollection<ClinicalProtocolDataGridItem> DataGridItems
{
get { return (ObservableCollection<ClinicalProtocolDataGridItem>)GetValue(DataGridItemsProperty); }
set { SetValue(DataGridItemsProperty, value); }
}
internal static readonly DependencyProperty DataGridItemsProperty =
DependencyProperty.Register("DataGridItems", typeof(ObservableCollection<ClinicalProtocolDataGridItem>),
typeof(ClinicalProtocolView), new PropertyMetadata(null, DataGridItemsPropertyChangedCallback));
private static void DataGridItemsPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (ClinicalProtocolView)d;
control.DataGridItems = (ObservableCollection<ClinicalProtocolDataGridItem>)e.NewValue;
}
對應類ClinicalProtocolDataGridItem
:
public class ClinicalProtocolDataGridItem
{
public string RtRoiInterpretedTypeCode { get; }
public string RtRoiInterpretedTypeDescription { get; }
public object SelectedStructureId { get; set; }
public ClinicalProtocolDataGridItem(string rtRoiInterpretedTypeCode, string rtRoiInterpretedTypeDescription)
{
RtRoiInterpretedTypeCode = rtRoiInterpretedTypeCode ??
throw new ArgumentNullException(nameof(rtRoiInterpretedTypeCode));
RtRoiInterpretedTypeDescription = rtRoiInterpretedTypeDescription ??
throw new ArgumentNullException(nameof(rtRoiInterpretedTypeDescription));
}
}
最后是依賴屬性ComboBoxItems
:
public ObservableCollection<ComboBoxItem> ComboBoxItems
{
get { return (ObservableCollection<ComboBoxItem>)GetValue(ComboBoxItemsProperty); }
set { SetValue(ComboBoxItemsProperty, value); }
}
public static readonly DependencyProperty ComboBoxItemsProperty =
DependencyProperty.Register("ComboBoxItems", typeof(ObservableCollection<ComboBoxItem>),
typeof(ClinicalProtocolView), new PropertyMetadata(new ObservableCollection<ComboBoxItem>(),
PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (ClinicalProtocolView)d;
control.ComboBoxItems = (ObservableCollection<ComboBoxItem>)e.NewValue;
}
我嘗試使用以下內容填充兩個依賴屬性:
private void AddSomeComboBoxItems()
{
ComboBoxItems = new ObservableCollection<ComboBoxItem>
{
new ComboBoxItem(){Content = "S1", HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center},
new ComboBoxItem(){Content = "S2", HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center},
new ComboBoxItem(){Content = "S3", HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center},
}
private void AddSomeRows()
{
var items = new List<ClinicalProtocolDataGridItem>
{
new ClinicalProtocolDataGridItem("PTV", "Description of PTV, and it is a very long description!"),
new ClinicalProtocolDataGridItem("OAR", "Description of OAR, and it is a very long description!"),
};
DataGridItems = new ObservableCollection<ClinicalProtocolDataGridItem>(items);
}
當我將它匯入 WPF 應用程式時,我得到以下資訊:
和錯誤資訊:
DataGridComboBoxColumn.ItemsSource IEnumerable Cannot find governing FrameworkElement or FrameworkContentElement for target element.
Microsoft 檔案說可以系結ComboBoxItem
. 我錯過了什么?我不想擁有靜態資源,因為組合框專案可能會在運行時發生變化。
uj5u.com熱心網友回復:
DataGridColumn 是一個簡單的 DependencyObject,而不是 UI 元素。它沒有嵌入到可視化樹中,無法從中獲取值。ElementName 和 FindAncestor 系結在其中不起作用。
您需要將集合放入靜態資源中,然后才從那里為 DataGridColumn 獲取它。
<DataGrid Name="ClinicalProtocolDataGrid"
ItemsSource="{Binding DataGridItems, ElementName=Clinical_Protocol}"
AutoGenerateColumns="False">
<FrameworkElement.Resources>
<!--Using a CollectionViewSource as a proxy to create a "static link".-->
<CollectionViewSource
x:Key="comboBoxItems"
Source="{Binding ComboBoxItems, ElementName=Clinical_Protocol}"/>
</FrameworkElement.Resources>
<DataGrid.Columns>
<DataGridComboBoxColumn
Header="Structure ID"
ItemsSource="{Binding Source={StaticResource comboBoxItems}}"
SelectedItemBinding="{Binding SelectedStructureId, Mode=TwoWay}"/>
PS請注意我對你的問題的評論。我在那里解釋了為什么即使在成功系結到這樣的源之后,ComboBox 仍可能無法正常作業。
uj5u.com熱心網友回復:
ComboBoxItems
洗掉不需要的依賴屬性,但將其添加到組合框的資料提供程式方法背后的代碼中:
public ObservableCollection<string> GetCbxSource()
{
return new ObservableCollection<string>
{
"S1",
"S2",
"S3",
};
}
然后在 XAML 中,您可以宣告一個資料提供程式,然后像這樣使用它:
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="cbxData" MethodName="GetCbxSource" ObjectInstance="{x:Reference Clinical_Protocol}"/>
</Grid.Resources>
<DataGrid Name="ClinicalProtocolDataGrid"
ItemsSource="{Binding DataGridItems, ElementName=Clinical_Protocol}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Structure ID"
ItemsSource="{Binding Source= {StaticResource cbxData}}"
SelectedItemBinding="{Binding SelectedStructureId, Mode=TwoWay}"
/>
<DataGridTextColumn Header="RT ROI Type Code"
Binding="{Binding RtRoiInterpretedTypeCode}"/>
<DataGridTextColumn Header="RT ROI Type Description"
Binding="{Binding RtRoiInterpretedTypeDescription}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/537156.html