带有DataTemplate的ListBox的SelectedItem

时间:2012-08-05 21:32:26

标签: wpf listbox datatemplate selecteditem

我有一个ListBox:

<ListBox Name="lbsfHolder" 
         ItemsSource="{Binding UISupportingFunctions}"
         SelectedItem="{Binding Path=SelectedSupportedFunction, Mode=TwoWay}"
         SelectionMode="Multiple"
         IsSynchronizedWithCurrentItem="True"
         HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <controls:SupportingFunction GotFocus="SupportingFunction_GotFocus"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在ViewModel中我有:

private SupportingFunction _selectedSupportedFunction;

public SupportingFunction SelectedSupportedFunction
{
    get { return _selectedSupportedFunction; }
    set
    {
        _selectedSupportedFunction = value;
        NotifyPropertyChanged("SelectedSupportedFunction");
    }
}

但是,当我尝试选择列表框中的任何项目时,没有任何反应。对于ListBox和SelectedValue,SelectedItem也为null。我是否需要添加一些特殊代码才能使其正常工作?

UPD:

我已经改变了一些观点,现在我有了:

<UserControl x:Class="RFM.UI.WPF.Controls.SupportingFunction">
    <Grid>
        <ListBox Name="supportingFunctions"
                 ItemsSource="{Binding UISupportingFunctions}"
                 SelectedItem="{Binding Path=SelectedSupportedFunction, Mode=TwoWay}"
                 SelectionMode="Multiple"
                 IsSynchronizedWithCurrentItem="True"
                 HorizontalContentAlignment="Stretch">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="30" />
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="30" />
                                    </Grid.ColumnDefinitions>
                                    <TextBox Name="tbsfName" Grid.Column="0" Text="{Binding Path=Title, Mode=TwoWay}"></TextBox>
                                    <TextBox Name="tbsfExperssion" Grid.Column="1" Text="{Binding Path=Expression}" HorizontalAlignment="Stretch"></TextBox>
                                    <Button Name="bsfDel" Grid.Column="2">Del</Button>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</UserControl>

在此控件所在的页面中:

<StackPanel Name="spSupportingFunctions">
            <StackPanel Name="spsfOperations" Orientation="Horizontal">
                <Button Name="bsfAdd" Width="30" Command="commands:CustomCommands.AddSupportingFunction">Add</Button>
            </StackPanel>
            <controls:SupportingFunction DataContext="{Binding Self}" />                
        </StackPanel>

在本页后面的代码

public PlotDataPage()
    {
        DataContext = new PlotDataViewModel();
        InitializeComponent();            
    }

这是PlotDataViewModel

的完整列表
public class UISupportingFunction : ISupportingFunction
{
    public string Title { get; set; }
    public string Expression { get; set; }
}

public class PlotDataViewModel : INotifyPropertyChanged
{
    public PlotDataViewModel Self
    {
        get
        {
            return this;
        }
    }

    private ObservableCollection<UISupportingFunction> _supportingFunctions;
    public ObservableCollection<UISupportingFunction> UISupportingFunctions
    {
        get
        {
            return _supportingFunctions;
        }
        set
        {
            _supportingFunctions = value;
            NotifyPropertyChanged("UISupportingFunctions");
        }
    }       

    private UISupportingFunction _selectedSupportedFunction;
    public UISupportingFunction SelectedSupportedFunction
    {
        get
        {
            return _selectedSupportedFunction;
        }
        set
        {
            _selectedSupportedFunction = value;
            NotifyPropertyChanged("SelectedSupportedFunction");
        }
    }

    public PlotDataViewModel()
    {
        UISupportingFunctions = new ObservableCollection<UISupportingFunction>();           
    }

    public void CreateNewSupportingFunction()
    {
        UISupportingFunctions.Add(new UISupportingFunction() { Title = Utils.GetNextFunctionName() });
    }

    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }

    public event PropertyChangedEventHandler PropertyChanged;       
}

当我单击Add按钮时,我只是调用CreateNewSupportingFunction()方法。一切都很好 - 项目是添加,我看到它们。但是,当我点击其中一个TextBox,然后点击每个项目的bsfDel按钮时,我在SelectedSupportedFunction中得到null。

也许是因为焦点事件已经被TextBox处理而不是ListBox?

1 个答案:

答案 0 :(得分:0)

您的ItemsSource UISupportingFunctions不是SupportingFunction对象,或者您没有将View的Datacontext设置为ViewModel。

ViewModel.xaml.cs

this.DataContext = new ViewModelClass();