单击项目模板上的事件

时间:2016-06-06 08:17:55

标签: c# wpf binding listbox

我的列表框项目有这个wpf代码:

 <ListBox x:Name="icTodoList" ItemsSource="{Binding ListControleMachine}" Grid.Column="3">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="{Binding borderColor }" BorderThickness="2" Margin="0,0,0,1">
            <Grid Margin="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="120" />
                    <ColumnDefinition Width="130" />
                    <ColumnDefinition Width="120" />
                    <ColumnDefinition Width="120" />
                    <ColumnDefinition Width="120" />
                    <ColumnDefinition Width="120" />
                    </Grid.ColumnDefinitions>
                    <CheckBox x:Name="check_action" Content="" HorizontalAlignment="Left"  VerticalAlignment="Top"  IsChecked="{Binding ActionCheked,Mode=TwoWay}"/>
                    <Ellipse x:Name="E_Ping"  HorizontalAlignment="Left" Fill="{Binding PingDotColor}" Height="10" Width="10" Margin="2,4,0,0" VerticalAlignment="Top"  RenderTransformOrigin="0.2,-1.182"   Grid.Column="1"/>
                    <TextBlock  Text="{Binding titlePing}" Grid.Column="2" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"   />
                    <TextBlock  Text="{Binding titlePing}" Grid.Column="2" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"   />
                    <TextBlock  Text="{Binding titleDcai}" Grid.Column="3"   MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock  Text="{Binding titleAd}" Grid.Column="4"   MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock  Text="{Binding FPACtitle}" Grid.Column="5"  MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}" />
                    <TextBlock  Text="{Binding titleMcAfee}" Grid.Column="6"  MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}" />
                    <TextBlock  Text="{Binding gkUser}" Grid.Column="7" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"  />
                    <TextBlock  Text="{Binding titleservicestat}" Grid.Column="8" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"  />
                 </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

正如您所看到的,我在每个文本框上都有一个“TextBlock_MouseDown”

然后是c#代码。

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
     TextBlock t = sender as TextBlock;
     DetailedView.DataContext =  ListControleMachine.Where(x => x.ComputerName == t.Tag.ToString());
     DetailedView.Visibility = System.Windows.Visibility.Visible;  
}

好的,所以它做了我需要的,基本上它会弹出一个视图,并将数据重新绑定到它。

但我很确定它不是一个干净的方法。

首先我不会在每个文本块上发生事件,但在listboxitem上它不会自行(但我不知道如何以这种方式传递计算机名称参数)。

第二,可能是我唯一的问题我认为我不必搜索我的收藏并用这个重新绑定我的新观点:

DetailedView.DataContext =  ListControleMachine.Where(x => x.ComputerName == t.Tag.ToString());

要清楚如何点击项目模板并弹出一个包含实际列表框项目绑定数据的视图?

1 个答案:

答案 0 :(得分:0)

尝试通过绑定到ListBox的SelectedItem来设置DataContext

<DetailedView DataContext="{Binding Path=SelectedItem, ElementName=icTodoList, Mode=OneWay}"/>

或等效代码

var binding = new Binding
{
    Path = new PropertyPath("SelectedItem"),
    ElementName = "icTodoList", 
    Mode = BindingMode.OneWay
};
BindingOperations.SetBinding(DetailedView, Control.DataContextProperty, binding);

<小时/> 另一种方法

ListBox有SelectionChanged个事件

<ListBox Name="icTodoList" 
            SelectionChanged="IcTodoList_OnSelectionChanged">

</ListBox>

事件处理程序

private void IcTodoList_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DetailedView.DataContext = icTodoList.SelectedItem;
    DetailedView.Visibility = System.Windows.Visibility.Visible;  
}

Tag值和TextBlock_MouseDown处理程序将不是必需的