Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何将按钮创建代码移动到样式中_Wpf - Fatal编程技术网

Wpf 如何将按钮创建代码移动到样式中

Wpf 如何将按钮创建代码移动到样式中,wpf,Wpf,希望将我的按钮创建代码转换为样式。。。但不完全确定如何定义某些元素(如高度,它取决于网格高度)和鼠标事件触发器 目前的代码如下: button.HorizontalAlignment = HorizontalAlignment.Stretch; button.VerticalAlignment = VerticalAlignment.Stretch; button.Name = buttonName; button.Margin =

希望将我的按钮创建代码转换为样式。。。但不完全确定如何定义某些元素(如高度,它取决于网格高度)和鼠标事件触发器

目前的代码如下:

        button.HorizontalAlignment = HorizontalAlignment.Stretch;
        button.VerticalAlignment = VerticalAlignment.Stretch;
        button.Name = buttonName;
        button.Margin = new Thickness(2);
        button.Padding = new Thickness(2);

        // We want the ellipse to have the same height and width, so let's calculate
        // the smallest of the two calculated measurements and then apply that to both of them
        var potentialHeight = (wGrid.Height / wGrid.Columns) - (button.Margin.Top * 2);
        var potentialWidth = (wGrid.Width / wGrid.Rows) - (button.Margin.Left * 2);
        button.Height = button.Width = (potentialHeight < potentialWidth) ? potentialHeight : potentialWidth;

        button.SetValue(Grid.ColumnProperty, column);
        button.SetValue(Grid.RowProperty, row);
        button.MouseEnter += Button_MouseEnter;
        button.PreviewMouseLeftButtonDown += PreviewButton_MouseLeftButtonDown;
        button.PreviewMouseLeftButtonUp += PreviewButton_MouseLeftButtonUp;
        button.MouseLeave += Button_MouseLeave;

        var style = Utilities.ResourceDictionary["WButton"] as Style;
        button.Style = style;
        SetButtonBackground(button);
button.HorizontalAlignment=HorizontalAlignment.Stretch;
button.VerticalAlignment=VerticalAlignment.Stretch;
button.Name=buttonName;
按钮。边距=新厚度(2);
按钮。填充=新厚度(2);
//我们希望椭圆具有相同的高度和宽度,所以让我们计算
//两个计算出的测量值中最小的一个,然后将其应用于两个测量值
var potentialHeight=(wGrid.Height/wGrid.Columns)-(button.Margin.Top*2);
var potentialWidth=(wGrid.Width/wGrid.Rows)-(button.Margin.Left*2);
button.Height=button.Width=(potentialHeight
类似这样的东西

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Column" Value="{Binding Column}"/>
            <Setter Property="Grid.Row" Value="{Binding Row}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="Button">
                <Button.Style>
                    <Style TargetType="Button" BasedOn="{StaticResource WButton}">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="VerticalAlignment" Value="Stretch"/>
                        <Setter Property="Margin" Value="2"/>
                        <Setter Property="Padding" Value="2"/>
                        <EventSetter Event="MouseEnter" Handler="Button_MouseEnter"/>
                        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="PreviewButton_MouseLeftButtonDown"/>
                        <EventSetter Event="PreviewMouseLeftButtonUp" Handler="PreviewButton_MouseLeftButtonUp"/>
                        <EventSetter Event="MouseLeave" Handler="Button_MouseLeave"/>
                    </Style>
                </Button.Style>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


最终结果应该是什么?一个正方形按钮的网格,每个按钮执行相同的操作?如果没有更多的上下文,就很难判断您实际应该做什么。答案是“是”。请使用ItemsControl,其中网格位于ItemsPanel中,按钮位于ItemTemplate中。还有一个ItemContainerStyle用于绑定Grid.Row和Grid.Colum属性。ItemsSource属性将绑定到一个数据项集合,其中包含各个按钮的所有必要数据。你能想出一个非常简单的示例Clemens吗?有人告诉我类似的事情,但我不能很好地拼凑出ItemContainerStyle、Panel和source是如何组合在一起的:\n对不起,您的问题中没有足够的上下文,请写出有用的答案。Grid.Column和Grid.Row绑定不能这样工作。您必须将它们转换为ItemContainerStyle。谢谢@Neil B-这对我来说非常有意义:)