【问题标题】:How to reach button objects which were created in ItemsControl如何访问在 ItemsControl 中创建的按钮对象
【发布时间】:2016-07-04 22:42:39
【问题描述】:

我在 UWP 中这样制作按钮:

        <ItemsControl x:Name="tStack" Grid.Row="1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate x:Name="iPT">
                <StackPanel x:Name="tStack1" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Height="64" Width="64"  ToolTipService.ToolTip="{Binding Name}" Click="Button_Click" Tag="{Binding AppUri}" Background="{Binding Color}"  >
                    <StackPanel>

                        <Image Source="{Binding IconUri}"></Image>
                    </StackPanel>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

现在,在单击它们中的每一个后,我想将单击的一个的背景设置为某种颜色,并将其余的背景颜色设置为透明。有任何想法吗?

【问题讨论】:

  • 我建议您将 IsClicked 属性保留在模型中,并根据模型使用触发器或转换器更改颜色。
  • 好的,但是如何从其他按钮重置'isClicked'属性?

标签: c# xaml uwp


【解决方案1】:

您可以通过将其保存到变量来跟踪当前活动的按钮。然后将所有按钮绑定到同一个 Click-function。当单击任何按钮时,您将重置活动按钮的背景,将变量更改为单击的按钮并将其背景设置为所需的颜色。

Button activeButton;

void Button_Click(Object sender, RoutedEventArgs e)
{
    activeButton.Background = null;
    activeButton = (Button)sender;
    activeButton.Background = Brushes.Blue;
}

【讨论】:

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