【问题标题】:How to get a control in a listbox on code behind windows phone?如何在 Windows Phone 后面的代码的列表框中获取控件?
【发布时间】:2012-09-24 09:59:33
【问题描述】:

我的 Windows Phone 应用程序中有一个列表框。在列表框 DataTemplate 中,我放置了一个按钮。如何在代码隐藏中获取按钮对象。我没有在 .cs 文件中获得 rowButton 的引用。我想更改每行按钮的按钮背景颜色。如何在后面的代码中获取按钮引用?

我遵循我用于列表视图的代码。

<Grid  Height="530" Grid.Row="1" VerticalAlignment="Top" Margin="0,30,0,0">
            <ListBox Margin="0,0,0,0" Name="TransactionList">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button  Width="460" Height="150" Click="user_click" Name="rowButton" >
                            <Button.Content>
                                <StackPanel Orientation="Horizontal" Height="auto" Width="400">
                                    <Image Width="80" Height="80" Source="{Binding Type}"></Image>
                                    <StackPanel Orientation="Vertical" Height="150" Margin="20,0,0,0">
                                        <StackPanel Orientation="Horizontal" Height="40">
                                            <TextBlock Width="100" FontSize="22" Text="Name :" Height="40" ></TextBlock>
                                            <TextBlock Width="auto" FontSize="22" Text="{Binding Name}" Height="40" ></TextBlock>
                                        </StackPanel>
                                        <StackPanel Orientation="Horizontal" Height="40">
                                            <TextBlock Width="100" FontSize="22" Text="Date :" Height="40" ></TextBlock>
                                            <TextBlock Width="100" FontSize="22" Text="{Binding Date}" Height="40" ></TextBlock>
                                        </StackPanel>
                                        <StackPanel Orientation="Horizontal" Height="40">
                                            <TextBlock Width="100" FontSize="22" Text="Amount :" Height="40" ></TextBlock>
                                            <TextBlock Width="auto" FontSize="22" Text="{Binding Amount}" Height="40" ></TextBlock>
                                            <TextBlock Width="auto" FontSize="22" Text=" $" Height="40" ></TextBlock>
                                        </StackPanel>
                                    </StackPanel>
                                </StackPanel>
                            </Button.Content>
                        </Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

【问题讨论】:

  • 您要在 user_click 上引用还是在迭代列表框源列表时引用?

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


【解决方案1】:

如果您想在用户点击时更改背景,请在点击事件处理程序中使用

            Button button1 = sender as Button;
            button1.Backgorund = new SolidColorBrush(Colors.Red);

改变背景颜色。

为每个按钮绑定背景属性,并在列表框中的项目迭代时更改其值。

【讨论】:

    【解决方案2】:

    如果没有看到您是如何尝试在代码中访问它的,就不可能说出为什么您正在做的事情不起作用。

    但是如果你使用数据绑定来设置模板项的颜色会容易得多

    【讨论】:

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