获取绑定列表框中所选项目的值

时间:2015-07-05 06:15:23

标签: c# wpf mvvm listbox observablecollection

我希望在string中显示的所选项目的doubleclick事件中获得listbox值。我正在使用mvvm架构。 以下是代码:

<ListBox ItemsSource="{Binding RecentProjects}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock>
                         <Run Text="{Binding FileName}"/>
                         <Run Text="{Binding CreationTime}"/>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding SelectedCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>

我的ViewModel中的代码如下所示:

private ObservableCollection<FileItem> _recentProjects = new ObservableCollection<FileItem>();
public ObservableCollection<FileItem> RecentProjects
    {
        get { return _recentProjects; }
        set { _recentProjects = value; }
    }

public RelayCommand SelectedCommand { get; private set; }
private void Selected()
    {
        //string fileName = RecentProjects.SelectedItem ===> Need stringvalue of filename
    }

而且,在FileItem模型中,我有一个FileName和一个CreationTime属性。 我想在RecentProjects Observablecollection中获取selecteditem的FileName的字符串值。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您应该将属性定义为 FileItem _myItem; public FileItem MyItem { get { return _myItem; } set { _myItem = value; OnPropertyChanged("MyItem"); } } private void Selected() { string fileName = MyItem.FileName; }

您的观看型号:

<ListBox ItemsSource="{Binding RecentProjects}" SelectedItem="{Binding MyItem}">
        <ListBox.ItemTemplate> ...

你的Xaml:

/* Rickshaw dashboard chart */
    var seriesData = [ [], [] ];
    var random = new Rickshaw.Fixtures.RandomData(1000);

    for(var i = 0; i < 100; i++) {
        random.addData(seriesData);
    }

    var rdc = new Rickshaw.Graph( {
            element: document.getElementById("dashboard-chart"),
            renderer: 'area',
            width: $("#dashboard-chart").width(),
            height: 250,
            series: [{color: "#B4D278",data: seriesData[0],name: 'New'}, 
                     {color: "#588007",data: seriesData[1],name: 'Returned'}]
    } );

    rdc.render();

    var legend = new Rickshaw.Graph.Legend({graph: rdc, element: document.getElementById('dashboard-legend')});
    var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({graph: rdc,legend: legend});
    var order = new Rickshaw.Graph.Behavior.Series.Order({graph: rdc,legend: legend});
    var highlight = new Rickshaw.Graph.Behavior.Series.Highlight( {graph: rdc,legend: legend} );        

    var rdc_resize = function() {                
            rdc.configure({
                    width: $("#dashboard-chart").width(),
                    height: $("#dashboard-chart").height()
            });
            rdc.render();
    }

    var hoverDetail = new Rickshaw.Graph.HoverDetail({graph: rdc});

    window.addEventListener('resize', rdc_resize);        

    rdc_resize();
    /* END Rickshaw dashboard chart */


/* Vector Map */
    var jvm_wm = new jvm.WorldMap({container: $('#dashboard-map-seles'),
                                    map: 'us_aea_en', 
                                    backgroundColor: '#FFFFFF',                                      
                                    regionsSelectable: true,
                                    regionStyle: {selected: {fill: '#589a35'},
                                                    initial: {fill: '#588007'}},
                                    markerStyle: {initial: {fill: '#435F0A',
                                                   stroke: '#435F0A'}},
                                    markers: [{latLng: [50.27, 30.31], name: 'Kyiv - 1'},                                              
                                              {latLng: [52.52, 13.40], name: 'Berlin - 2'},
                                              {latLng: [48.85, 2.35], name: 'Paris - 1'},                                            
                                              {latLng: [51.51, -0.13], name: 'London - 3'},                                                                                                      
                                              {latLng: [40.71, -74.00], name: 'New York - 5'},
                                              {latLng: [35.38, 139.69], name: 'Tokyo - 12'},
                                              {latLng: [37.78, -122.41], name: 'San Francisco - 8'},
                                              {latLng: [28.61, 77.20], name: 'New Delhi - 4'},
                                              {latLng: [41.88, -87.63], name: 'Chicago - 6'},
                                              {latLng: [32.77, -96.79], name: 'Dallas - 1'},
                                              {latLng: [50.85, 4.35], name: 'Brussels'},
                                              {latLng: [39.91, 116.39], name: 'Beijing - 3'}]
                                });    
    /* END Vector Map */


/* Donut dashboard chart */
    Morris.Donut({
        element: 'dashboard-donut-1',
        data: [
            {label: "Returned", value: 1513},
            {label: "New", value: 764},
            {label: "Unique", value: 300},
            {label: "Registered", value: 1311},
            {label: "Guests", value: 250}
        ],
        colors: ['#588007', '#fea223', '#435F0A', '#b64645', '#FFF'],
        resize: true
    });
    /* END Donut dashboard chart */