C# 从项目模板控件事件获取ListBoxItem
在下面的WPF XAML代码中,如果我处于模板按钮的SelectTaskItemClick事件中,如何获取当前选中的ListBoxItem ItemSource对象C# 从项目模板控件事件获取ListBoxItem,c#,wpf,xaml,styles,C#,Wpf,Xaml,Styles,在下面的WPF XAML代码中,如果我处于模板按钮的SelectTaskItemClick事件中,如何获取当前选中的ListBoxItem ItemSource对象 <!-- ListBox ITEMS --> <TaskDash:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3" ItemsSource="{Bindin
<!-- ListBox ITEMS -->
<TaskDash:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3"
ItemsSource="{Binding}">
<!--ItemsSource="{Binding}" DisplayMemberPath="Description">-->
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Path=Selected}"/>
</Style>
<TaskDash:ListBoxWithAddRemove.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button DockPanel.Dock="Left" Click="SelectTaskItemClick">SELECT</Button>
<TextBox DockPanel.Dock="Left" Name="EditableDescription" Text="{Binding Description}" Height="25" Width="100" />
<Button DockPanel.Dock="Left" Click="EditTaskItemClick">EDIT</Button>
</DockPanel>
</DataTemplate>
</TaskDash:ListBoxWithAddRemove.ItemTemplate>
</TaskDash:ListBoxWithAddRemove>
在上面的代码中,我猜它是从App.XAML中获取的,其中定义了自定义ListBoxWithAddRemove控件。如何遍历实际表单的XAML[上面显示的第一个代码]
<Style x:Key="{x:Type TaskDash:ListBoxWithAddRemove}" TargetType="{x:Type TaskDash:ListBoxWithAddRemove}">
<Setter Property="Margin" Value="3" />
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TaskDash:ListBoxWithAddRemove}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Grid.Row="0"
Click="DeleteControlClick">Delete</Button>
<Button Grid.Column="1" Grid.Row="0"
Click="AddControlClick">Add</Button>
<Border
Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
Name="Border"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1"
CornerRadius="2">
<ScrollViewer
Margin="0"
Focusable="false">
<StackPanel Margin="0" IsItemsHost="True" />
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
删除
添加
如果您有一个正确类型的对象,例如
private void SelectTaskItemClick(object sender, RoutedEventArgs e)
{
var b = sender as Button;
DependencyObject item = b;
while (item is ListBoxItem == false)
{
item = VisualTreeHelper.GetParent(item);
}
var lbi = (ListBoxItem)item;
//...
}
(如果您只想选择可以(也应该)通过已建立的绑定完成的项目,例如)
假设
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Path=Selected}"/>
</Style>
按照预期的方式工作,您应该能够循环浏览DataContext中用作列表框的ItemsSource的项目,并检查每个项目的Selected属性以查找当前选定的项目。从列表框中确定所选项目的更典型方法是使用ListBox.SelectedItem,其中ListBox是一个引用相关列表框的变量。或者,您可以通过
SelectTaskItemClick
方法的sender
参数访问它。您可以尝试的另一种方法是帮助器方法,用于遍历可视化树,如和中所述。我认为按钮的作用是选择单击的项目,因此事先不会选择它。谢谢,这正是我所需要的。看起来我还需要删除Style元素。我一直得到一个DataContext为STYLE而不是TASKITEM的ListBoxItem:@Shawn:样式本身很好,但您不应该像以前那样将其添加到ListBox,而是将其设置为其ItemContainerStyle
:
private void SelectTaskItemClick(object sender, RoutedEventArgs e)
{
// The DataContext should be an item of your class that should
// have a Selected property as you bind to it in a style.
var data = (sender as FrameworkElement).DataContext as MyClass;
data.Selected = true;
}
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Path=Selected}"/>
</Style>