Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataTemplate项绑定到DataGrid的SelectedItem_C#_Wpf_Data Binding_Datatemplate - Fatal编程技术网

C# 将DataTemplate项绑定到DataGrid的SelectedItem

C# 将DataTemplate项绑定到DataGrid的SelectedItem,c#,wpf,data-binding,datatemplate,C#,Wpf,Data Binding,Datatemplate,我在绑定TextBox文本时遇到问题,该文本位于DataTemplate标记内 有ItemsControl绑定到observedcollection controls集合。这里有代码(包括DataGrid: <DataTemplate x:Key="TextBoxDataTemplate"> <Grid VerticalAlignment="Top" HorizontalAlignment="Left"> <TextBlock Vertica

我在绑定
TextBox
文本时遇到问题,该文本位于
DataTemplate
标记内

ItemsControl
绑定到
observedcollection controls集合
。这里有代码(包括
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>
最后,这是我填充
控件集合的方式:

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}"/>

正确。如何绑定此
文本框
以获取
SelectedProduct
属性(当然有选择更改)?每个
SelectedProduct
属性都有
OnPropertyChanged()
trigger.

因此,在ItemsControl中,您希望将文本框的文本绑定到主视图模型的
SelectedProduct
属性,而不是绑定到项目的
ControlValue
?@Clemens类似的内容。文本框的文本应该绑定到
ControlValue
。真正的问题是。如何从
Se传递值每次我更改选择时,将SelectedProduct
添加到
ControlValue
中?好吧,通过编写代码。WPF并没有为该任务提供内置解决方案。@克莱门斯和我非常接近它。使用
ControlValue=SelectedProduct.GetType().GetProperty(controlProp).GetValue(SelectedProduct,null).ToString();
仅在开头设置值。您能告诉我如何为其提供“更新”吗?当然有一个SelectedProduct属性设置程序。在那里更新关联的ControlModel对象的ControlValue。
 <TextBox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="205,0,0,0" Width="616" Height="35" FontSize="12" Text="{Binding ControlValue,UpdateSourceTrigger=PropertyChanged}"/>