我在绑定TextBox
标记内的DataTemplate
文本时遇到问题。
有ItemsControl
绑定到ObservableCollection<ControlModel> ControlsCollection
。那里是代码(包含DataGrid
:
<DataTemplate x:Key="TextBoxDataTemplate">
<Grid VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Width="200" FontSize="15" FontWeight="Bold" Text="{Binding ControlName}" TextWrapping="Wrap"/>
<TextBox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="205,0,0,0" Width="616" Height="35" FontSize="12" Text="{Binding ControlValue,UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</DataTemplate>
<ItemsControl ItemsSource="{Binding ControlsCollection}" ItemTemplateSelector="{StaticResource ControlTemplateSelector}"/>
<DataGrid Margin="25,100,25,50" ItemsSource="{Binding ProductSpecificationView}"
EnableColumnVirtualization="True" EnableRowVirtualization="True" SelectedItem="{Binding SelectedProduct,UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single" IsReadOnly="True" CellStyle="{StaticResource MyDataGridCell}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="col1" Binding="{Binding Index,UpdateSourceTrigger=PropertyChanged}"
/>
<DataGridTextColumn Width="*" Header="col2" Binding="{Binding IndexName,UpdateSourceTrigger=PropertyChanged}"
/>
<DataGridTextColumn Width="*" Header="col3" Binding="{Binding TechnologyStructure,UpdateSourceTrigger=PropertyChanged}"
/>
<DataGridTextColumn Width="*" Header="col4" Binding="{Binding TechVariants,UpdateSourceTrigger=PropertyChanged}"
/>
<DataGridTextColumn Width="*" Header="col5" Binding="{Binding TechTwoVariants,UpdateSourceTrigger=PropertyChanged}"
/>
</DataGrid.Columns>
</DataGrid>
...和ControlModel:
public class ControlModel : ViewModel
{
private string controlName;
public string ControlName
{
get { return controlName; }
set
{
controlName = value;
OnPropertyChanged("ControlName");
}
}
private string controlValue;
public string ControlValue
{
get { return controlValue; }
set
{
controlValue = value;
OnPropertyChanged("ControlValue");
}
}
public string ControlType { get; set; }
public ControlModel()
{
}
}
最后,这就是我填充ControlsCollection
的方式:
public void GetControlsCollection()
{
using (var conn = Connection.Connect())
{
string query = "select c.control_name, " +
"ct.control_type_name,c.control_property_name from Controls c " +
"inner join ControlsType ct on k.kontrolka_typ_id = tk.kontrolka_typ_id ";
using (SqlCommand cmd = new SqlCommand(query,conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string controlValue = "";
string controlProp = reader[2].ToString();
if (SelectedProduct != null)
{
controlValue = SelectedProduct.GetType().GetProperty(controlProp).GetValue(SelectedProduct, null).ToString();
}
ControlsCollection.Add(new ControlModel()
{
ControlName = reader[0].ToString(),
ControlType = reader[1].ToString(),
ControlValue = controlValue
});
}
}
}
}
}
我无法绑定...
<TextBox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="205,0,0,0" Width="616" Height="35" FontSize="12" Text="{Binding ControlValue,UpdateSourceTrigger=PropertyChanged}"/>
正确。如何绑定此TextBox
以获得SelectedProduct
属性(当然会更改选择)?每个SelectedProduct
属性都有
OnPropertyChanged()
触发器。