Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
C# 图像按钮上的边框_C#_.net_Wpf_Xaml - Fatal编程技术网

C# 图像按钮上的边框

C# 图像按钮上的边框,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个带有按钮的动态网格,这些按钮就像图像一样。出于设计目的,我希望能够在这些图片周围有一个边框 下面是代码: EmployeeButton employeeTile = new EmployeeButton(); // inherits from Button, nothing fancy, just added three attributes for future use Style style = this.FindResource("NoChromeButton") as Style

我有一个带有按钮的动态网格,这些按钮就像图像一样。出于设计目的,我希望能够在这些图片周围有一个边框

下面是代码:

EmployeeButton employeeTile = new EmployeeButton(); // inherits from Button, nothing fancy, just added three attributes for future use
Style style = this.FindResource("NoChromeButton") as Style;
employeeTile.Style = style;

// make it square (like a tile)
employeeTile.Width = Properties.Settings.Default.tileWidthInPx;
employeeTile.Height = employeeTile.Width;

// Create Background
var brush = new ImageBrush();
brush.ImageSource = item.Source; // item is an Image (part of a foreach)
employeeTile.Background = brush;

// set the margin between tiles
int margin = Properties.Settings.Default.tileMargin;
employeeTile.Margin = new Thickness(margin, margin, margin, 0);

// draw a border if the user wants to
if (Properties.Settings.Default.tileBorderThickness > 0)
{
    employeeTile.BorderBrush = Brushes.Black;
    employeeTile.BorderThickness = new Thickness(Properties.Settings.Default.tileBorderThickness);
}
为了便于阅读,我删除了一些与我的问题无关的行

下面是我上面使用的样式的xaml代码。我想是在stackoverflow的某个地方找到的

<Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                            <Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


无论我设置的厚度有多大,都没有边框,只有可单击的图像。

您的控件模板应该如下所示:

<ControlTemplate TargetType="{x:Type Button}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
        <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>
    </Border>

    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#ADADAD"/>
            <Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>


在代码中设置BorderBrush和BorderThickness时,它将设置组件的BorderBrush和BorderThickness,但在模板中,您不使用这些属性,因此不会使用它们。您只需要在ControlTemplate中使用这些属性添加边框。

您的按钮是否具有边框属性?否则,您将需要创建边框元素,设置其大小/形状,并将图像/按钮嵌套在该边框元素内。我看到的唯一属性是BorderBrush和BorderThickness,这就是我尝试设置它们的原因。因为我是动态创建这些按钮的,所以首先必须创建边框,然后将按钮作为子按钮添加?类似(短):border border=newborder();border.children.add(按钮)@是的,这两个都是“边界”属性,所以它确实有边界。边界笔刷设置在哪里?您可能需要将按钮/图像嵌套在它自己的边框元素中:
“stuff in border”
我只能使用代码隐藏,因为这些按钮必须动态创建。XAML解决方案很简单,但我做不到。当前逻辑看起来像是创建了边框,但不可见,因为图像位于上层。我想在图像上创建边框并增加边距,这样它们就不会重叠。我不太喜欢那种变通方法。在c#中,我刚刚创建了边框,并将图像作为子对象添加,但仍然只有图像可见。这突出了我关于图像和边界重叠的假设@CalebB
我有一个动态网格,带有类似图像的按钮
-删除所有可怕的代码并使用
项目控件
。非常感谢您的澄清。我刚刚做到了,而且效果很好。:)祝你今天愉快