【问题标题】:Selecteditem event in an itemtemplate inside a listbox列表框中项目模板中的 Selecteditem 事件
【发布时间】:2012-02-15 06:18:39
【问题描述】:

我需要在列表框项目模板中设置选择更改事件。我的列表框由三个文本块和一个图像组成。我只想获取第三个文本块文本,当我选择第三个文本块时,文本块中的文本将显示为弹出窗口。

我使用可视化树搜索文本块,但它采用第一个文本块的值而不是第三个文本块的值。我该怎么做才能获得第二个和第三个文本块的值。只有当我单击列表框中的文本块而不是整个列表框项时,我才需要触发弹出窗口。

<ListBox Name="listBox1" Width="Auto" SelectionChanged="Listbox1_SelectionChanged"> 
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Height="165" HorizontalAlignment="Left" Margin="10,40,-400,0"  VerticalAlignment="Top" Width="175" Source="{Binding thumb}"/>
                <TextBlock Name="pagetext"  TextWrapping="Wrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-135,-200,0" Text="{Binding page}" Foreground="#FF170101" />
                <TextBlock Name="titletext" Width="1000" TextWrapping="NoWrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-167,-200,0" Text="{Binding title}" Foreground="#FF170101" />
                <TextBlock Name="text" Width="1000" TextWrapping="NoWrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-167,-200,0" Text="{Binding title}" Foreground="#FF170101" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>   
</ListBox>  

【问题讨论】:

    标签: c# wpf silverlight windows-phone-7 xaml


    【解决方案1】:

    使用 TextBlock_MouseLeftButtonUp 或 TextBlock_Tap(点击文本块时上升)

    在这,

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock t = (TextBlock)sender;
            string s= t.Text;
        }
    

    上面的字符串显示在你想要的地方。

    【讨论】:

      【解决方案2】:

      您可能应该将有问题的TextBlock 包装在Button 中,或者在其中添加Hyperlink。两者都支持命令并具有Click 事件。 (要使Button 不可见,您可以将Template 覆盖为仅简单的ContentPresenter

      <Button>
          <Button.Template>
              <ControlTemplate TargetType="Button"><ContentPresenter/></ControlTemplate>
          </Button.Template>
          <TextBlock .../>
      </Button>
      
      <TextBlock>
          <!-- In SL you probably need a Run inside the Hyperlink and it may not be bindable -->
          <Hyperlink Text="{Binding title}" .../>
      </TextBlock>
      

      【讨论】:

        猜你喜欢
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多