1

我的 xaml 文件中有一个名为“mystack”的堆栈面板,我正在从 .cs 文件中动态添加按钮,并希望删除 C# .cs 文件中按钮的边框

我真正想要的是用来自字符串列表的按钮填充这个堆栈面板,谢谢

xml:

    <Grid HorizontalAlignment="Left" Height="227" Margin="10,10,0,0" Grid.Row="2"    
        VerticalAlignment="Top" Width="530">
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <StackPanel Name="mystack" HorizontalAlignment="Left" Grid.Row="2" 
                       VerticalAlignment="Top" Width="520"/>
        </ScrollViewer>
    </Grid>

。CS:

     public List<String> Schools()
    {

        List<String> l = new List<string>();
        l.Add("SST");
        l.Add("SBE");
        l.Add("SSH");

        return l;

    }
4

5 回答 5

1

我同意 HighCore,您通常不想在代码中操作 UI 元素。

要删除Border按钮,您可以在 Xaml 或代码隐藏中将 Button 的BorderThickness属性设置为“0” 。new Thickness(0)

IE

myButton.BorderThickness = new Thickness(0);

编辑:

好的,我注意到你更新的问题。我将创建一个属性来存储您的学校列表并以类似于以下方式绑定到它:

public List<string> Schools 
{ 
    get { return _schools; }
    set { _schools = value; } 
}

您需要在某处DataContext将控件设置为包含该Schools属性的类。如果您要动态更新 Schools 列表,则需要实现INotifyPropertyChanged以便 UI 知道何时更新。然后你的 Xaml 看起来像这样:

<ItemsControl ItemsSource="{Binding Schools}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
             <Button Content="{Binding}" BorderThickness="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
<ItemsControl>
于 2013-03-27T17:22:21.800 回答
0

快速修复:

我必须做些什么来有效地hide按钮边框 - 由于按钮控制模板我相信它利用和更改按钮边框(即即使你删除它它会在我相信的某个触发器上绘制它)......

...也要设置BorderBrush="Transparent"(我总是这样做BorderThickness,但我猜这不是必需的 - 仅用于视觉/布局外观)

thickness单独设置是不够的。

我真的不确定这是否是下注的方法,或者实际上我很确定一定有更聪明的东西 - 但我总是以这种方式结束。

正确的方式:

正确的方法 - 并推荐 - 是编写自己的 Button 模板 - 基于 Microsoft 官方模板 - 或base它上面 - 并做你需要的无边框。

对于/C# 后面的代码:

根据您更改的问题,您真的不需要那个 - 做其他人已经建议的事情

于 2013-03-27T17:30:34.033 回答
0

您不能删除按钮的边框,例如:btn.BorderThicknes=new Thickness(0);

看到这个:删除按钮的边框

于 2013-03-27T17:33:04.177 回答
0

最好的方法是:

<Style TargetType="Button">
    <Style.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="0"/>
        </Style>
    </Style.Resources>
</Style>
于 2015-08-08T09:59:03.333 回答
-1

我真正想要的是用来自字符串列表的按钮填充这个堆栈面板

这被称为ListBox

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
           <Button Content="{Binding}" BorderThickness="0"/>
                   <!-- Whatever other customizations to the button -->
        </DataTemplate
    </ListBox.ItemTemplate>
</ListBox>

视图模型:

public class ViewModel
{
    public ObservableCollection<string> Items {get;set;}

    public ViewModel()
    {
        Items = new ObservablecCollection<string>();
        Items.Add("String1");
        Items.Add("String2");
        Items.Add("String3");
    }
}

您需要学习 MVVM 模式。

于 2013-03-27T17:28:05.013 回答