如何将ComboBox的SelectedItem作为DataTemplate内部的字符串?

时间:2015-12-11 13:34:12

标签: c# wpf

我有一个DataTemplate,其中包含ComboBox,它来自我的模型。我一直试图获得SelectedText ComboBox但失败了。尝试了其他答案中提到的几乎所有内容。

这是我的XAML代码:

<DataTemplate x:Name="myTemplate">
    <ComboBox x:Name="ActionCombo" SelectedIndex="{Binding ActionId, Mode=TwoWay}" IsTextSearchEnabled="True"
              IsEditable="True" Text="Select Action" SelectionChanged="ActionCombo_SelectionChanged" 
              Initialized="ActionCombo_Initialized" ItemsSource="{Binding}" DisplayMemberPath="Action"/>
</DataTemplate>

这是模特:

class Actions
{
     public int ID { get; set; }
     public string Action { get; set; }
}

actions.Add(new Actions() { ID= 0, Action="Issued" });
actions.Add(new Actions() { ID = 1, Action = "Resolved" });
actions.Add(new Actions() { ID = 2, Action = "Pending" });

这三个项目填充ComboBox上的page_Load

我尝试Selection_Changed事件的方法:

string txt = (((sender as ComboBox).SelectedItem) as ComboBoxItem).Name.ToString();
  

结果= NULL

string txt = (((ComboBox)sender).SelectedItem).ToString();
  

结果=&#34; WPFSample1App.Actions&#34;

string txt = (((ComboBox)sender).Text);
  

结果=&#34;选择操作&#34;

它总是SelectedItemAction模型(我不想要)。 怎么办?

修改

<DataGrid x:Name="strGrid" AutoGenerateColumns="False" CanUserAddRows="False" SelectedIndex="0">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="25"/>
        <DataGridTextColumn Header="Store" Binding="{Binding StoreName}" Width="*"/>
        <DataGridTextColumn Header="TimeIn" Binding="{Binding TimeIn}" Width="*"/>
        <DataGridTextColumn Header="TimeOut" Binding="{Binding TimeOut}" Width="*"/>
        <DataGridTemplateColumn Header="Action" Width="*" x:Name="comboTemp">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate x:Name="myTemplate">
                    <ComboBox x:Name="ActionCombo" SelectedIndex="{Binding ActionId, Mode=TwoWay}" IsTextSearchEnabled="True"
                             IsEditable="True" Text="Select Action" SelectionChanged="ActionCombo_SelectionChanged" 
                             Initialized="ActionCombo_Initialized" ItemsSource="{Binding}" DisplayMemberPath="Action">
                       <ComboBoxItem Content="{Binding Action}"/>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

每个ComboBox都会在Initialized事件

上填充
private void ActionCombo_Initialized(object sender, EventArgs e)
{
     ComboBox combo = sender as ComboBox;
     combo.ItemsSource = actions;
}

DataGridpage_load上填充了不同的来源(数据库)

2 个答案:

答案 0 :(得分:1)

你的第二种方法非常接近,试试这个

string txt = (((sender as ComboBox)?.SelectedItem) as Actions)?.Action?.ToString();

如果文本为null,那么你可以做这样的事情

if(txt == null)
{
     txt = (sender as ComboBox).Text;
}

请注意我没有测试过,但我不明白为什么它不起作用。

答案 1 :(得分:1)

而不是Name使用Content这样:

string txt = (((sender as ComboBox).SelectedItem) as ComboBoxItem).Content.ToString();

您的XAML中也错过了ComboBoxItem。它应该是这样的:

<ComboBox x:Name="ActionCombo" SelectedIndex="{Binding ActionId, Mode=TwoWay}" IsTextSearchEnabled="True"
        IsEditable="True" Text="Select Action" SelectionChanged="ActionCombo_SelectionChanged" 
        Initialized="ActionCombo_Initialized" ItemsSource="{Binding}" DisplayMemberPath="Action">
      <ComboBoxItem Content="{Binding Action}"></ComboBoxItem>
</ComboBox>

<小时/> 如果您不使用<ComboBoxItem Content="{Binding Action}"/>,则应使用以下代码获得结果:

string txt = (((sender as ComboBox).SelectedItem) as Actions).Action; .