开发者

how do I access a radiobutton inside a ListBoxItem in windows phone 7

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>
                    </DataTemp开发者_如何转开发late>
                </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.


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; }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜