【问题标题】:how do I access a radiobutton inside a ListBoxItem in windows phone 7如何在 Windows Phone 7 中访问 ListBoxItem 内的单选按钮
【发布时间】:2011-02-11 07:46:02
【问题描述】:

请查看我正在使用的 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;

请帮帮我。

【问题讨论】:

  • 您想在什么时候访问它以及为哪个RadioButton?附带说明,我不认为 ListBoxItem 可以使用 ItemTemplate 选择
  • 我想获取选定的RadioButton 的内容并将其存储在隔离存储中,如果此ItemTemplate 没有帮助,那么您能否建议我正确的方法。这是使用上述代码生成的 GUI 屏幕截图。 link

标签: c# silverlight windows-phone-7 listboxitem


【解决方案1】:

您可以使用 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; }
    }
}

【讨论】:

  • 我想将content 属性中的string 存储到文本文件中的隔离存储中。
  • 那你为什么不直接保存你绑定到内容的项目呢?因为它直接代表内容。
  • 这取决于用户的选择。看一下屏幕截图here,现在假设用户单击Personal Expense RadioButton 然后我希望字符串Personal Expense 存储在文件中。希望我对你说清楚
  • 然后您应该将 RadioButton 的 IsChecked 属性绑定到 itemssource 项目的布尔属性。例如,您有一个具有 2 个属性的类:名称和选定。然后将 itemssource 绑定到该类的集合,并将内容属性绑定到 Name,将 IsChecked 属性绑定到 Selected。然后你就可以查询你的收藏并获取选中的项目。
  • 嘿 Luc,当我现在尝试运行它时,它会在 var items=(List&lt;Accounts&gt;)listBoxDefaultAcc.ItemSource; 行抛出 InvalidCastException 请帮帮我。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多