从其模板中的按钮修改ListBox的项目?

时间:2010-03-28 15:03:51

标签: c# wpf silverlight data-binding xaml

在我的Silverlight 3项目中,我使用ListBox来显示WCF WebService的Get()操作的结果。

ListBox的项目模板如下:

<ListBox x:Name="m_listview" ItemsSource="{Binding Users, Mode=TwoWay, UpdateSourceTrigger=Default}" Foreground="{StaticResource EnergyBlue}" Background="{StaticResource EnergyBackground}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" Grid.ColumnSpan="3" Margin="0,0,0,2" IsSynchronizedWithCurrentItem="False">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid MinWidth="536" MinHeight="10" d:DesignWidth="19.875" d:DesignHeight="20.75">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.2*"/>
                            <ColumnDefinition Width="0.2*"/>
                            <ColumnDefinition Width="0.2*"/>
                            <ColumnDefinition Width="0.2*"/>
                            <ColumnDefinition Width="0.2*"/>
                        </Grid.ColumnDefinitions>
                        <TextBox Text="{Binding Path=UserName, Mode=TwoWay, UpdateSourceTrigger=Default}" Grid.Column="0" Foreground="{StaticResource EnergyWhite}" Margin="2" Background="{StaticResource EnergyBackground}" BorderBrush="{StaticResource EnergyBlue}" SelectionBackground="{StaticResource EnergyBlue}" SelectionForeground="{StaticResource EnergyWhite}" MinWidth="100"/>
                        <PasswordBox Password="{Binding Path=UserPass, Mode=TwoWay, UpdateSourceTrigger=Default}" Grid.Column="1" Foreground="{StaticResource EnergyWhite}" Margin="2" Background="{StaticResource EnergyBackground}" BorderBrush="{StaticResource EnergyBlue}" SelectionBackground="{StaticResource EnergyBlue}" SelectionForeground="{StaticResource EnergyWhite}" MinWidth="100"/>
                        <TextBox Text="{Binding Path=UserTypeId, Mode=TwoWay, UpdateSourceTrigger=Default}" Grid.Column="2" Foreground="{StaticResource EnergyWhite}" Margin="2" Background="{StaticResource EnergyBackground}" BorderBrush="{StaticResource EnergyBlue}" SelectionBackground="{StaticResource EnergyBlue}" SelectionForeground="{StaticResource EnergyWhite}" MinWidth="100"/>
                        <Button Style="{StaticResource EnergyGlassButton}" Grid.Column="3" MinWidth="10" MinHeight="10" Content="Update" Click="OnUpdateUser"/>
                        <Button Style="{StaticResource EnergyGlassButton}" Grid.Column="4" MinWidth="10" MinHeight="10" Content="Remove" Click="OnRemoveUser"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

模板包含2个按钮。当我点击其中任何一个时,我想获得对listboxitem下的确切数据项的引用。我怎么做?我试过了:

User target_user = m_listview.SelectedItem as User;

但事实证明未选择列表视图项。可以在没有实际选择listview项目的情况下完成吗?例如,只需单击“删除”按钮并使该行消失?

祝你好运

1 个答案:

答案 0 :(得分:2)

我相信以下内容应该检索您需要的项目: -

void OnUpdateUser(object sender, RoutedEventArgs e)
{    
  object item = ((Button)sender).DataContext;
  // do stuff with item
}