【问题标题】:Bind button command in listboxItemlistboxItem 中的绑定按钮命令
【发布时间】:2016-01-04 10:59:16
【问题描述】:

我想从一个列表框项绑定一个按钮命令。但我的代码不起作用。你能帮帮我吗?

我的项目模板定义:

<UserControl.Resources>
    <DataTemplate x:Key="MemberList">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="20"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
            </Grid.RowDefinitions>

            <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Source="{DynamicResource appbar_user}" Height="30" Width="30" VerticalAlignment="Center" HorizontalAlignment="Left"/
            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding EMail}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
            <Button Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" Width="25" Height="25" VerticalAlignment="Center" IsDefault="False" Content="X">
            </Button>
        </Grid>
    </DataTemplate>
</UserControl.Resources>

还有我的列表框声明:

<ListBox Margin="0,0,10,0" Grid.Column="0" Grid.Row="2" x:Name="_ownersList" ItemsSource="{Binding GroupOwners}" ItemTemplate="{DynamicResource MemberList}" >
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
                <EventSetter Event="Button.Click" Handler="Button_Click"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

感谢您的帮助。

【问题讨论】:

  • 我刚刚复制了你的代码,这对我来说很好。
  • 我刚刚复制了你的代码,这对我来说很好。我在窗口中使用它,我在 XAML 中定义了窗口 DataContext。但我不认为这是问题所在。尝试使用窥探来找出代码的真正问题。我认为有一个元素可以处理按钮单击事件。告知您是否需要帮助...

标签: wpf listbox command listboxitem commandbinding


【解决方案1】:

对我来说效果很好

XAML:

 <Window.Resources>
        <DataTemplate x:Key="MemberList">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="25"/>
                </Grid.RowDefinitions>

                <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Source="{DynamicResource appbar_user}" Height="30" Width="30" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding EMail}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <Button Grid.Column="2" Command="{Binding DataContext.CloseButton,RelativeSource={RelativeSource AncestorType=Window, AncestorLevel=1}}" Grid.Row="0" Grid.RowSpan="2" Width="25" Height="25" VerticalAlignment="Center" IsDefault="False" Content="X">
                </Button>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid Height="200" Width="200" VerticalAlignment="Center">
        <ListBox  Margin="0,0,10,0" Grid.Column="0" Grid.Row="2" x:Name="_ownersList" ItemsSource="{Binding GroupOwners}" ItemTemplate="{DynamicResource MemberList}" >
            <!--<ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="Button.Click" Handler="Button_Click"/>
                </Style>
            </ListBox.ItemContainerStyle>-->
        </ListBox>

    </Grid>

视图模型

private ICommand closeButton;       
public ICommand CloseButton
{
    get
    {
        if (this.closeButton == null)
        {
            this.closeButton = new RelayCommand<object>(this.ExecuteCloseButton);
        }

        return this.closeButton;
    }
}

private void ExecuteCloseButton(object err)
{

}

【讨论】:

  • 我已尝试修改,但不适用于我的解决方案。 :(
  • 将 AncestorType=Windows 更改为 AncestorType=UserControl
  • 我已经被 UserControl 改变了,但是什么都没有。我没有错误,但命令没有执行。
  • 数据是否显示在ListBox上?
  • 是显示数据。
猜你喜欢
  • 2013-10-09
  • 2016-06-25
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 2015-06-07
  • 2013-06-07
相关资源
最近更新 更多