C# 组合框文本未绑定到DataGrid SelectedItem

C# 组合框文本未绑定到DataGrid SelectedItem,c#,wpf,xaml,combobox,datagrid,C#,Wpf,Xaml,Combobox,Datagrid,我希望我的组合框的Text属性根据DataGrid的SelectedItem进行更改。我尝试过代码隐藏和XAML,并且已经做到了这一点 <ComboBox Grid.Row="6" Grid.Column="1" x:Name="contactEmployeeComboBox" Text="{Binding SelectedItem.EmployeeName, ElementName=contactsDataGrid, Mode=OneWay}" Margin="5"> 尝试设置

我希望我的组合框的
Text
属性根据
DataGrid
SelectedItem
进行更改。我尝试过代码隐藏和
XAML
,并且已经做到了这一点

<ComboBox Grid.Row="6" Grid.Column="1" x:Name="contactEmployeeComboBox" Text="{Binding SelectedItem.EmployeeName, ElementName=contactsDataGrid, Mode=OneWay}" Margin="5">

尝试设置为双向,如下所示:

<ComboBox Text="{Binding SelectedItem.EmployeeName, ElementName=contactsDataGrid, Mode=TwoWay}"/>
代码隐藏:

public class YourModel
{
    public string TitleField { get; set; }    
}
    public MainWindow()
    {
        InitializeComponent();
        FillDataGrid();
    }

    private void FillDataGrid()
    {
        ObservableCollection<YourModel> coll = new ObservableCollection<YourModel>();
        for (int start = 0; start < 10; start++)
        {
            coll.Add(new YourModel(){TitleField="Title " +  
            start.ToString());                                
        }
        dataGrid.ItemsSource = coll;
        comboBox.DisplayMemberPath = "TitleField";
        comboBox.ItemsSource = coll;
    }


    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        var dataGrid = e.Source as DataGrid;
        var currentIndex = dataGrid.Items.IndexOf(dataGrid.CurrentItem);            
        comboBox.SelectedIndex= currentIndex;
    }
<StackPanel>
   <DataGrid Name="dataGrid" SelectionChanged="dataGrid_SelectionChanged" />
   <ComboBox  Name="comboBox"/>
</StackPanel>   
public主窗口()
{
初始化组件();
FillDataGrid();
}
私有void FillDataGrid()
{
ObservableCollection coll=新的ObservableCollection();
对于(int start=0;start<10;start++)
{
coll.Add(新的YourModel(){TitleField=“Title”+
start.ToString());
}
dataGrid.ItemsSource=coll;
comboBox.DisplayMemberPath=“TitleField”;
comboBox.ItemsSource=coll;
}
private void dataGrid_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{            
var dataGrid=e.源为dataGrid;
var currentIndex=dataGrid.Items.IndexOf(dataGrid.CurrentItem);
comboBox.SelectedIndex=currentIndex;
}
您的XAML:

public class YourModel
{
    public string TitleField { get; set; }    
}
    public MainWindow()
    {
        InitializeComponent();
        FillDataGrid();
    }

    private void FillDataGrid()
    {
        ObservableCollection<YourModel> coll = new ObservableCollection<YourModel>();
        for (int start = 0; start < 10; start++)
        {
            coll.Add(new YourModel(){TitleField="Title " +  
            start.ToString());                                
        }
        dataGrid.ItemsSource = coll;
        comboBox.DisplayMemberPath = "TitleField";
        comboBox.ItemsSource = coll;
    }


    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        var dataGrid = e.Source as DataGrid;
        var currentIndex = dataGrid.Items.IndexOf(dataGrid.CurrentItem);            
        comboBox.SelectedIndex= currentIndex;
    }
<StackPanel>
   <DataGrid Name="dataGrid" SelectionChanged="dataGrid_SelectionChanged" />
   <ComboBox  Name="comboBox"/>
</StackPanel>   


设置为
Text=“{Binding SelectedItem.EmployeeName,ElementName=contactsDataGrid,Mode=TwoWay}”
是MVVM还是代码隐藏?请输入您的填充代码
DataGrid
DataGrid
ComboBox
没有填充,它只有两项我硬编码的内容。我已经添加了
DataGrid
的代码,尽管我认为这是无关的…我说它是MVVM应用程序对吗?@Steppup no这是代码落后,不幸的是仍然没有运气,而且我实际上想要
单向
,这样
数据网格
就不会随着
组合框的更改而更新
@CBreeze请随意询问任何问题question@CBreeze请查看我的更新答案。如果对您有帮助,您可以将我的答复标记为答案,以加快未来的搜索速度其他人的