C# 绑定到切换按钮

C# 绑定到切换按钮,c#,wpf,C#,Wpf,我有一个xaml页面,上面有30个togglebutton,我需要将每个togglebutton的4个属性绑定到我拥有的类。我可以做绑定,但我希望找到更好的解决方案 我的类当前看起来像这样: public int ToggleButton1Height; public int ToggleButton1Width; .. public int ToggleButton2Height; public int ToggleButton2Width; ..etc 如您所见,如果每个togglebut

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

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

public int ToggleButton1Height;
public int ToggleButton1Width;
..
public int ToggleButton2Height;
public int ToggleButton2Width;
..etc

如您所见,如果每个togglebutton有4个属性,这意味着我的类中需要120多个属性。有更好的方法吗?

我对您的场景有点好奇,但下面是我能想到的最简单的解决方案。注释中解释了所有代码,但是如果您有任何问题,请告诉我。当然,关于如何做到这一点还有很多解决方案,但我没有太多关于你想做什么的细节。所以我希望这对你有用。您应该能够将所有这些复制粘贴到一个新项目中并使其正常工作

XAML:


C#:

使用System.Windows;
使用System.Windows.Controls.Primitives;
命名空间堆栈溢出测试
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
//创建所有类并设置每个ToggleButton的DataContext
已加载私有无效窗口1_(对象发送方,路由目标)
{
int-sizeFactor=0;
//对于StackPanel中的每个ToggleButton,创建ToggleButtonClass的一个实例
//并将其分配给ToggleButton的DataContext,因此样式中的所有绑定
//在XAML中创建可以生效。
foreach(StackPanel.Children中的UIElement元素)
{
if(元素为ToggleButton)
{
sizeFactor++;
ToggleButtonClass ToggleButtonClass=新建ToggleButtonClass()
{
ToggleButtonHeight=sizeFactor*20,
ToggleButtonWidth=sizeFactor*50,
ToggleButtonText=“Button”+SizeActor
};
(元素为ToggleButton)。DataContext=toggleButtonClass;
}
}
}
}
//您的切换按钮类
公共类ToggleButtonClass
{
公共双切换按钮右键{get;set;}
公共双切换按钮宽度{get;set;}
公共字符串ToggleButtonText{get;set;}
}
}
<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>
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; }
    }
}