我在绑定方面遇到了一点问题。我想将 2 个按钮与SelectedItem
a绑定ListBox
。
包含几个值并显示ListBox
它们(姓名,年龄,...)。当用户选择 的一个条目时ListBox
,应弹出 2 个按钮“删除”和“更改”(<- 可见),因此这意味着按钮可见性应与 的 绑定,SelectedItem
并且ListBox
位于 ListBox-Item 旁边(如 iPhone 的“删除”按钮)。
当我选择一个项目时,我的代码实际上使按钮可见,但问题是所有项目的所有按钮都是可见的!实际代码(需要修复)是这样的(可见性部分):
<Button DataContext="{Binding DataContext, ElementName=Window}" FontSize="15" Grid.Row="0" Content="Delete" Visibility="{Binding Path=SelectedItem, Mode=OneWay, ElementName=lbuser, Converter={StaticResource VisibilityConverter}}" Command="{Binding Delete}"/>
<Button DataContext="{Binding DataContext, ElementName=Window}" FontSize="15" Grid.Row="1" Content="Change" Visibility="{Binding Path=SelectedItem, Mode=OneWay, ElementName=lbuser, Converter={StaticResource VisibilityConverter}}" Command="{Binding Change}" />
但是如果有人需要完整的代码,这里是:
<Grid Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="212"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="15"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" x:Name="lbuser" ItemsSource="{Binding Users, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.ColumnSpan="2" SelectedItem="{Binding MySelectedListView, Mode=TwoWay}" Height="390" Width="Auto" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Bezeichnung}" Grid.ColumnSpan="4" FontSize="22.5" FontWeight="ExtraBold"/>
<TextBlock Grid.Row="1" Text="Name"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Name}"/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="Age"/>
<TextBlock Grid.Row="1" Grid.Column="4" Text="{Binding Path=Age, Converter={StaticResource EuroConverter}}"/>
<TextBlock Grid.Row="2" Text="E-Mail"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=EMail}" Width="Auto"/>
<TextBlock Grid.Row="2" Grid.Column="2" Text="Phone"/>
<TextBlock Grid.Row="2" Grid.Column="4" Text="{Binding Path=Phone, Converter={StaticResource EuroConverter}}"/>
<TextBlock Grid.Row="3" Text="Born"/>
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Path=YearBorn}" Width="Auto"/>
<TextBlock Grid.Row="3" Grid.Column="2" Text="Birthplace"/>
<TextBlock Grid.Row="3" Grid.Column="4" Text="{Binding Path=Birthplace}"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button DataContext="{Binding DataContext, ElementName=Window}" FontSize="15" Grid.Row="0" Content="Delete" Visibility="{Binding Path=SelectedItem, Mode=OneWay, ElementName=lbuser, Converter={StaticResource VisibilityConverter}}" Command="{Binding Delete}"/>
<Button DataContext="{Binding DataContext, ElementName=Window}" FontSize="15" Grid.Row="1" Content="Change" Visibility="{Binding Path=SelectedItem, Mode=OneWay, ElementName=lbuser, Converter={StaticResource VisibilityConverter}}" Command="{Binding Change}" />
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</Grid>
编辑:可见性转换器
public class VisibilityConverter : IValueConverter
{
#region [ IValueConverter ]
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return System.Windows.Visibility.Collapsed;
return System.Windows.Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
该值是选定项,这意味着当 ListBox 有 2 个项目并且我单击一个项目时,VisibilityConverter 将被调用 4 次并将 Visibility 设置为 true。通过转换器的 4 次调用,该值始终是 SelectedItem。