首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >绑定到ToggleButton

绑定到ToggleButton
EN

Stack Overflow用户
提问于 2010-06-29 08:44:18
回答 1查看 354关注 0票数 1

我有一个带有30个togglebutton的xaml页面,我需要将每个togglebutton的4个属性绑定到我拥有的一个类上。我可以做绑定,但我希望找到一个更好的解决方案。

我的类目前看起来像这样:

代码语言:javascript
运行
复制
public int ToggleButton1Height;
public int ToggleButton1Width;
..
public int ToggleButton2Height;
public int ToggleButton2Width;
..etc

如你所见,如果我为每个togglebutton设置了4个属性,这意味着我的类中需要超过120个属性。有没有更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-29 10:09:48

我对你的场景有点好奇,但这是我能想到的最快的简单解决方案。所有的代码在评论中都有解释,但是如果你有问题一定要让我知道。当然,关于如何做到这一点,还有更多的解决方案,但我没有太多关于你想要做什么的细节。所以我希望这对你有用。您应该能够将所有这些内容复制粘贴到一个新项目中,并使其正常工作。

XAML:

代码语言:javascript
运行
复制
<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1"
    x:Name="window1"
    Width="800"
    Height="600"
    Loaded="Window1_Loaded">
    <Window.Resources>
        <!-- 
            Create a style so you don't have to define the properties 30 times.
            The style will tell the ToggleButtons which properties in your class they should look at.
            The ToggleButtonClass object will be bound on the code side
        -->
        <Style TargetType="{x:Type ToggleButton}" x:Key="toggleButtonStyle">
            <Setter Property="Height" Value="{Binding ToggleButtonHeight}" />
            <Setter Property="Width" Value="{Binding ToggleButtonWidth}" />
            <Setter Property="Content" Value="{Binding ToggleButtonText}" />
        </Style>
    </Window.Resources>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <StackPanel x:Name="theStackPanel">
            <!--
                You still have to set the style in each button, in case you add buttons that you don't want to use the properties
                If you don't want to manually set the style, remove the x:Key="toggleButtonStyle" property from the style above
                and also remove the Style="{StaticResource toggleButtonStyle}" from all the toggle buttons below.
            -->         
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            <ToggleButton Style="{StaticResource toggleButtonStyle}" />
        </StackPanel>
    </ScrollViewer>
</Window>

C#:

代码语言:javascript
运行
复制
using System.Windows;
using System.Windows.Controls.Primitives;

namespace StackOverflowTests
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        // Creating all the classes and setting the DataContext of each ToggleButton
        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            int sizeFactor = 0;

            // for each ToggleButton in the StackPanel, create one instance of the ToggleButtonClass
            // and assign it to the DataContext of the ToggleButton, so all the binding in the Style
            // created in XAML can kick in.
            foreach (UIElement element in theStackPanel.Children)
            {
                if (element is ToggleButton)
                {
                    sizeFactor++;

                    ToggleButtonClass toggleButtonClass = new ToggleButtonClass()
                    {
                        ToggleButtonHeight = sizeFactor * 20,
                        ToggleButtonWidth = sizeFactor * 50,
                        ToggleButtonText = "Button " + sizeFactor
                    };

                    (element as ToggleButton).DataContext = toggleButtonClass;
                }
            }
        }
    }

    // your toggle button class
    public class ToggleButtonClass
    {
        public double ToggleButtonHeight { get; set; }
        public double ToggleButtonWidth { get; set; }
        public string ToggleButtonText { get; set; }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3137135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档