簡體   English   中英

綁定到ToggleButton

[英]Binding to ToggleButton

我有一個帶有30個切換按鈕的xaml頁面,我需要將每個切換按鈕的4個屬性綁定到我擁有的類。 我可以進行綁定,但是我希望找到更好的解決方案。

我的班級目前看起來像這樣:

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

如您所見,每個切換按鈕是否都有4個屬性,這意味着我的課堂上需要120多個屬性。 有沒有更好的辦法?

我對您的情況有點好奇,但這是我能想到的最快的簡單解決方案。 注釋中解釋了所有代碼,但是如果您有任何疑問,請告訴我。 當然,有關如何執行此操作還有很多其他解決方案,但是對於您要執行的操作,我沒有很多詳細信息。 因此,我希望這對您有用。 您應該能夠將所有這些內容復制粘貼到一個新項目中並使其起作用。

XAML:

<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#:

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; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM