按下时将按钮更改为绑定颜色

发布于 2024-09-26 14:45:34 字数 1759 浏览 0 评论 0原文

我想在按下时更改按钮颜色:

            <DataTemplate>
                <!--Click="btn_Click"-->
                <ItemsControl DataContext="{Binding}" ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>

                            <Button 
                                Name="btn"
                                DataContext="{Binding}"                             
                                Height="65" Width="79"
                                Background="Gray"
                                >

                                <Button.Template>

                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Ellipse Name="elipse1" Height="65" Width="79" Fill="{Binding Path=ButtonColor}" Visibility="Collapsed"></Ellipse>
                                        <ControlTemplate.Triggers>
                                             <Trigger Property="IsPressed" Value="True">
                                                <Setter TargetName="elipse1" Property="Visibility" Value="Visible"/>

                                            </Trigger>
                                         </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>

                            </Button>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

似乎没有比我使用 all tr​​iger <> 更进一步的工作了。未显示灰色按钮是否该模板比绑定更强?这是真的吗?结局如何呢?

i want to change my button color when is pressed :

            <DataTemplate>
                <!--Click="btn_Click"-->
                <ItemsControl DataContext="{Binding}" ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>

                            <Button 
                                Name="btn"
                                DataContext="{Binding}"                             
                                Height="65" Width="79"
                                Background="Gray"
                                >

                                <Button.Template>

                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Ellipse Name="elipse1" Height="65" Width="79" Fill="{Binding Path=ButtonColor}" Visibility="Collapsed"></Ellipse>
                                        <ControlTemplate.Triggers>
                                             <Trigger Property="IsPressed" Value="True">
                                                <Setter TargetName="elipse1" Property="Visibility" Value="Visible"/>

                                            </Trigger>
                                         </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>

                            </Button>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

it doesnt seem to work farther then that wjen i use the all triger <> the gray button is not showen has if this template is stronger then the binding ?is it true ? how to outcome it ?

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

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

发布评论

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

评论(1

木有鱼丸 2024-10-03 14:45:34

问题是,当未按下按钮时 - 它没有视觉影响(Visibility =“Collapsed”) - 按钮内没有元素 - 因此它变得不可见(然后很难按下它:))。

您需要在 ControlTemplate 中添加除椭圆之外的其他内容,以使其显示某些内容而不是椭圆。

The problem is that when the button isn't pressed - it has no visual impact (Visibility="Collapsed") - there are no elements inside the Button - thus it becomes invisible (and then it is hard to Press it :)).

You need to put something besides the Ellipse in the ControlTemplate to have it show something instead of the ellipse.

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