【问题标题】:Access controls inside listbox c# (windows Phone 8)列表框内的访问控制 c# (windows Phone 8)
【发布时间】:2015-07-22 10:20:49
【问题描述】:
         my xaml  code..
      <phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="DataTemplate1" >
        <Border BorderBrush="LightGray" BorderThickness="1" Height="150"            Width="500" >
            <Grid Width="500" Height="150" Background="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.9*"/>
                    <ColumnDefinition Width="2.5*"/>
                    <ColumnDefinition Width="1.5*"/>

                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image Name="imgitem"  Grid.Column="0" Height="Auto" Width="Auto" Source="{Binding ImgSource}" Margin="0,5,4,4" HorizontalAlignment="Left" />
                <TextBlock x:Name="txtbindprice"  Text="{Binding _PRICE,ConverterCulture=en-IN,StringFormat=C}" TextWrapping="Wrap"  Grid.Column="1"  Width="350"  Foreground="Black" Height="60" Margin="40,70,20,-10"/>
                <TextBlock x:Name="txtFinalTotal"  Text="{Binding _FinalTotal}" TextWrapping="Wrap"  Grid.Column="1"  Width="350"  Foreground="Red" Height="60" Margin="40,115,20,-10"/>
                <TextBlock x:Name="txtITMNAME"  Text="{Binding _ITMNAME }" Padding="1"  Tap="ItmName_Tapped" TextDecorations="Underline" FontSize="24" TextWrapping="Wrap" Grid.Column="1"  FontWeight="Normal" TextTrimming="WordEllipsis"  Foreground="OrangeRed" Width="Auto"  Height="150"  Margin="30,25,10,-10"/>
                <CheckBox  Grid.Column="2" Grid.Row="0" Background="Black" Foreground="Black" BorderThickness="4" BorderBrush="Red" Margin="10,20,-15,10"  Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked"  /> 

            </Grid>
        </Border>
    </DataTemplate>

     <ListBox Height="Auto" Name="lstbxmanual" SelectionMode="Extended" ItemTemplate="{StaticResource DataTemplate1 }"  Width="475" Margin="2,192,0,-39" Background="White"  HorizontalAlignment="Left" Grid.RowSpan="2">
    </ListBox>

我想根据选定的索引访问列表框中的文本块

accessing textblock 
  string  a =txtbindprice.text

无法工作,因为它们在列表框的数据模板中..

我遇到了可视子树方法和其他一些示例..我找不到太多信息..

请帮我解决这个问题...

【问题讨论】:

  • 为什么视觉子树方法不适合你?你试过this answerthis one吗?
  • 根据我的要求,哪个更好??
  • 我会根据这些更新我的答案。
  • 谢谢您..请具体说明..这对我的学习目的很有用..

标签: c# .net xaml windows-phone-8


【解决方案1】:

以下方法查找当前选中的列表框项:

    private ListBoxItem FindSelectedListBoxItem(DependencyObject control)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is ListBoxItem && ((ListBoxItem)child).IsSelected)
            {
                // Found the control so return
                return (ListBoxItem)child;
            }

            else
            {
                // Not found it - search children
                ListBoxItem nextLevel = FindSelectedListBoxItem(child);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }

以下方法从给定的根控件中查找具有特定名称的任何子控件:

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
    int childNumber = VisualTreeHelper.GetChildrenCount(control);
    for (int i = 0; i < childNumber; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(control, i);
        FrameworkElement fe = child as FrameworkElement;
        // Not a framework element or is null
        if (fe == null) return null;

        if (child is T && fe.Name == ctrlName)
        {
            // Found the control so return
            return child;
        }
        else
        {
            // Not found it - search children
            DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
            if (nextLevel != null)
                return nextLevel;
        }
    }
    return null;
}

现在您可以使用第一种方法获取当前选定的ListBoxItem,然后将其传递给第二种方法以检索此ListBoxItem 中的任何控件(例如txtbindprice):

ListBoxItem item = FindSelectedListBoxItem(this);
TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
string a = txtPrice.Text;

更新

您可以像这样将SelectionChanged 事件添加到您的ListBox

 <ListBox Height="Auto" Name="lstbxmanual" SelectionChanged="OnSelectionChanged">
</ListBox>

然后以这种方式检索某个TextBlock.Text(例如txtbindprice):

public void OnSelectionChanged(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem item = FindSelectedListBoxItem((ListBox(sender)));
    TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
    string a = txtPrice.Text;
}

【讨论】:

  • 我正在寻找一个程序来获取列表框中所有文本块的值....我在列表框中有超过 100 个索引可供选择...
  • 您仍然不必从txtbindprice.text 获取它。如果您的ListBox 有一个ItemsSource,您可以通过循环100 次从这个ItemsSource 获取每个_PRICE
  • 我的列表框中的项目索引会有所不同,因为我正在为列表框实现搜索功能..所以项目来源有时会不匹配..
  • 我在列表框中有一堆项目......它正在检索不匹配的数据......如何根据 selecteditem 索引获取准确的数据
  • 什么意思? this inside FindSelectedListBoxItem(this) 是您的主窗口的一个实例,或者如果您在 selectionChanged 事件中使用此方法,您可以使用 (ListBox)sender 代替。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
相关资源
最近更新 更多