【问题标题】:Modify a ListBox's item from a button in it's template?从模板中的按钮修改 ListBox 的项目?
【发布时间】:2010-03-28 15:03:50
【问题描述】:

在我的 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;

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

最好的问候

【问题讨论】:

    标签: c# wpf silverlight data-binding xaml


    【解决方案1】:

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

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

    【讨论】:

    • 突然出现在我脑海中的快速问题:如果 ListBox 本来是 ModelView 的一个元素,并且按钮将附加到 ICommand 命令怎么办?那该怎么办呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多