listview的Datatemplate中的ComboBox无法正常工作

时间:2013-12-17 11:17:39

标签: c# wpf

“Listview”的“DataTemplate”中的“ComboBox”无法正常工作。

<ComboBox x:Name="colTopics"  Width="100" Height="30" SelectedValue="{Binding SELECTEDTOPICProperty}" Margin="31,333,317,10">
        <ComboBoxItem Content="eyteeee"/>
        <ComboBoxItem Content="eyteyte"/>
</ComboBox>

如果这个'combobox'在'ListView'的'DataTemplate'中,'SelectedValue'(属性'SELECTEDTOPICProperty')的绑定给出'null'值。 DataTemplate中的ComboBox是动态生成的。

<ListView  x:Name="gridTopics" BorderThickness="2" ItemsSource="{Binding Path=TOPICSINFO}" HorizontalAlignment="Left" Margin="91,79,0,0" VerticalAlignment="Top" Height="242" Width="310" BorderBrush="#FF32A3D6"  >
        <ListView.Effect>
            <DropShadowEffect Color="#FF9ADDFB"/>
        </ListView.Effect>
        <ListView.View>
            <GridView >
                <GridView.Columns>
                    <GridViewColumn  x:Name="colPageNo" DisplayMemberBinding="{Binding PAGENO}" Width="50" Header="Pages" />
                    <GridViewColumn x:Name="colListTopics" Width="241" Header="Associated Topics" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="colTopics"  Width="100" Height="30"  SelectedValue="{Binding SELECTEDTOPICProperty}" Margin="31,333,317,10">
                                <ComboBoxItem Content="eyteeee"/>
                                <ComboBoxItem Content="eyteyte"/>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

它工作正常,如果'ComboBox'不在'ListView'中,则在属性'SELECTEDTOPICProperty'中有选定的值。

public class TopicsInfo
{
    public TopicsInfo()
    {
        listTopics = new List<string>();
    }

    private string pageNo;

    public string PAGENO
    {
        get { return pageNo; }
        set { pageNo = value; }
    }

    private List<string> listTopics;

    public List<string> LISTTOPICS
    {
        get { return listTopics; }
        set { listTopics = value; }
    }
    private string selectedTopicString;
    public string SELECTEDTOPICProperty
    {
        get { return selectedTopicString; }
        set
        {
            selectedTopicString = value;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果可能的话,嘿尝试像这样重构你的代码,

你的财产类,

public class TopicsInfo
{
    private string pageNo;

    public string PAGENO
    {
        get { return pageNo; }
        set { pageNo = value; }
    }

    private string selectedTopicString;
    public string SELECTEDTOPICProperty
    {
        get { return selectedTopicString; }
        set
        {
            selectedTopicString = value;
        }
    }
}

要与ListView绑定的属性,

private ObservableCollection<TopicsInfo> topicsInfoCollection = new ObservableCollection<TopicsInfo>();

        public ObservableCollection<TopicsInfo> TopicsInfoCollection
        {
            get
            {
                return this.topicsInfoCollection;
            }

            set
            {
                this.topicsInfoCollection = value;
            }
        }

和您的XAML代码,

<ListView  x:Name="gridTopics" BorderThickness="2" ItemsSource="{Binding Path=TopicsInfoCollection}" HorizontalAlignment="Left" Margin="91,79,0,0" VerticalAlignment="Top" Height="242" Width="310" BorderBrush="#FF32A3D6"  >
        <ListView.Effect>
            <DropShadowEffect Color="#FF9ADDFB"/>
        </ListView.Effect>
        <ListView.View>
            <GridView >
                <GridView.Columns>
                    <GridViewColumn  x:Name="colPageNo" DisplayMemberBinding="{Binding PAGENO}" Width="50" Header="Pages" />
                    <GridViewColumn x:Name="colListTopics" Width="241" Header="Associated Topics" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="colTopics"  Width="100" Height="30"  SelectedValue="{Binding SELECTEDTOPICProperty}" Margin="31,333,317,10">
                                <ComboBoxItem Content="eyteeee"/>
                                <ComboBoxItem Content="eyteyte"/>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>