如何访问 Windows Phone 7 中 ListBoxItem 内的单选按钮

发布于 2024-10-17 10:51:37 字数 982 浏览 1 评论 0原文

请查看我正在使用的 ListBox 的代码

<ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="60" Width="450">
                            <RadioButton Content="{Binding}" GroupName="defaultAcc" HorizontalAlignment="Left" VerticalAlignment="Center" Height="80" Width="450" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

现在我想从代码隐藏访问 RadioButtoncontent 属性。

ListBoxItems 正在从代码隐藏中动态填充以下代码:

listBoxDefaultAcc.ItemsSource = from acc in db.Table<Accounts>()
                                        select acc.accName;

请帮我解决这个问题。

Please review the code for the ListBox I am using

<ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="60" Width="450">
                            <RadioButton Content="{Binding}" GroupName="defaultAcc" HorizontalAlignment="Left" VerticalAlignment="Center" Height="80" Width="450" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Now I want to access the content property of the RadioButton from codebehind.

The ListBoxItems are getting filled dynamically from the codebehind with the following code:

listBoxDefaultAcc.ItemsSource = from acc in db.Table<Accounts>()
                                        select acc.accName;

Please help me out with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-10-24 10:51:37

您可以使用 VisualTreeHelper 并深入到该控件。但不建议这样做。
更好的方法是仅绑定到数据模板中控件的属性,然后通过获取绑定值来检索值。从技术上讲,在这种情况下,如果您想要更改单选按钮的内容,那么您需要更改 itemssource 中的项目

您能解释一下您试图通过获取单选按钮的内容来归档什么吗?

编辑**********

  <ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="60" Width="450">
                            <RadioButton Content="{Binding Name}" IsChecked="{Binding Selected, Mode=TwoWay}" GroupName="defaultAcc" HorizontalAlignment="Left" VerticalAlignment="Center" Height="80" Width="450" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

public partial class Home : Page
{
    public Home()
    {
        InitializeComponent();
        var items = new List<SomeClass>();
        items.Add(new SomeClass() {Name = "a"});
        items.Add(new SomeClass() {Name = "b"});
        items.Add(new SomeClass() {Name = "c"});

        listBoxDefaultAcc.ItemsSource = items;
    }

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void testButton_Click(object sender, RoutedEventArgs e)
    {
        var items = (List<SomeClass>)listBoxDefaultAcc.ItemsSource;
        var selectedItem = items.Where(x => x.Selected).FirstOrDefault();
    }

    class SomeClass
    {
        public string Name { get; set; }
        public bool Selected { get; set; }
    }
}

You can use the VisualTreeHelper and drill down to the control. This is not recommended though.
Better is to only bind to the properties of the controls in you datatemplate and then retrieve the values by getting the binded values. Technically in this case, if you would want to change the content of the radiobutton then you would need to change the item in the itemssource

Can you explain what you are trying to archieve by getting the content of the radiobutton?

Edit**********

  <ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="60" Width="450">
                            <RadioButton Content="{Binding Name}" IsChecked="{Binding Selected, Mode=TwoWay}" GroupName="defaultAcc" HorizontalAlignment="Left" VerticalAlignment="Center" Height="80" Width="450" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

public partial class Home : Page
{
    public Home()
    {
        InitializeComponent();
        var items = new List<SomeClass>();
        items.Add(new SomeClass() {Name = "a"});
        items.Add(new SomeClass() {Name = "b"});
        items.Add(new SomeClass() {Name = "c"});

        listBoxDefaultAcc.ItemsSource = items;
    }

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void testButton_Click(object sender, RoutedEventArgs e)
    {
        var items = (List<SomeClass>)listBoxDefaultAcc.ItemsSource;
        var selectedItem = items.Where(x => x.Selected).FirstOrDefault();
    }

    class SomeClass
    {
        public string Name { get; set; }
        public bool Selected { get; set; }
    }
}
空城缀染半城烟沙 2024-10-24 10:51:37

您应该使用数据绑定。您应该将 Content 绑定到您设置为项目的对象的表示内容的属性。

这样,您就不必关心列表框或模板或任何东西。您只需操作对象,这些更改就会反映在 GUI 中。

You should be using DataBinding. You should bind Content to a property, that represents content, of an object, you are setting as item.

This way, you dont have to care about ListBoxes or Templates or anything. You are simply manipulating objects, and theese changes get reflected in the GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文