【问题标题】:WPF combobox two way binding with TextBlockWPF组合框与TextBlock的两种方式绑定
【发布时间】:2013-10-04 06:59:24
【问题描述】:

我创建了以下列表

List<cb> combolist = new List<cb>();

combolist.Add(new cb() { name = "Neo",bloodgroup = "O+ve"}); 

combolist.Add(new cb() { name = "meo", bloodgroup = "O" });

combolist.Add(new cb() { name = "bsv", bloodgroup = "B+ve" });

cboxnames.ItemsSource = combolist;

现在我正在创建一个组合框,它使用项目模板从上述列表中获取数据

<ComboBox Margin="12,31,421,258" Name="cboxnames" IsEditable="False">
  <ComboBox.ItemTemplate>
     <DataTemplate>
             <TextBlock Text="{Binding name}"/>
         </DataTemplate>
        </ComboBox.ItemTemplate>   
</ComboBox>

现在我正在创建一个额外的文本块,用于显示在组合框中选择的项目

<TextBlock Height="28" HorizontalAlignment="Left" Background="LightGray" Margin="0,138,0,0" Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=SelectedItem.Content}" VerticalAlignment="Top" Width="191" />

问题是每当我从组合框中选择一个项目时,该项目不会显示在文本块中,请帮助!!!

【问题讨论】:

    标签: wpf binding combobox textblock


    【解决方案1】:
    <TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
                              ElementName=cboxnames,
                              Mode=TwoWay,Path=SelectedItem.name}"/>
    

    使用这个..希望它可以帮助你

    【讨论】:

      【解决方案2】:

      您好,检查您的 TextBlock 绑定。它应该是 SelectedItem.name 而不是 SelectedItem.Content

      Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=**SelectedItem.name**}" 
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-05
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        • 1970-01-01
        • 2021-03-01
        • 1970-01-01
        • 2016-10-24
        相关资源
        最近更新 更多