所以我if let someView = array.first as? SomeView {
print(someView.foo2)
}
与ListView
CheckBox
:
Column
我的模型有多个<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<CheckBox Margin="0,0,0,0"/>
<TextBlock x:Name="textBlock"
Text="{Binding Name}"
Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
Style="{StaticResource TextBlockDefaultStyle}"
Margin="0,2,0,0"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
,例如properties
,Name
等,而我的Description
也包含简单的ListView
。
我的Checkbox
填充了几个对象,我希望实现两件事:
如果我检查具体的ListView
,我希望所有其他CheckBox
成为CheckBox
(如果我有另一个Uncheck
处于Checked状态)
如何确定特定的CheckBox
对象
答案 0 :(得分:1)
XAML:
<ListView x:Name="MyListView" SelectionChanged="MyListView_OnSelectionChanged" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox>
<CheckBox.IsChecked>
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
</CheckBox.IsChecked>
</CheckBox>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Description" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// some data for testing
MyListView.ItemsSource = new List<MyData>
{
new MyData {Name = "123", Description = "ABC"},
new MyData {Name = "456", Description = "EFG"},
new MyData {Name = "789", Description = "HIJ"},
};
}
private void MyListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
var selectedItem = (MyData)e.AddedItems[0];
// do something
}
}
}
public class MyData
{
public string Name { get; set; }
public string Description { get; set; }
}
添加:在检查其他CheckBox
时取消选中其他<CheckBox Checked="ToggleButton_OnChecked">
<CheckBox.IsChecked>
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
</CheckBox.IsChecked>
</CheckBox>
private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
{
foreach (var checkBox in FindAllCheckBoxes(MyListView).Where(x => !ReferenceEquals(x, sender)))
{
checkBox.IsChecked = false;
}
}
private static IEnumerable<CheckBox> FindAllCheckBoxes(Visual element)
{
if (element == null)
{
return null;
}
var checkBoxes = new List<CheckBox>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
var checkBox = visual as CheckBox;
if (checkBox != null)
{
checkBoxes.Add(checkBox);
}
else
{
checkBoxes.AddRange(FindAllCheckBoxes(visual));
}
}
return checkBoxes;
}
的笨拙方式。
/**
* Product tag trail.
*/
private function add_crumbs_product_tag() {
$current_term = $GLOBALS['wp_query']->get_queried_object();
$this->prepend_shop_page();
//$this->add_crumb( sprintf( __( 'Products tagged “%s”', 'woocommerce' ), $current_term->name ) );
$this->add_crumb( sprintf('%s', $current_term->name ) );
}