【问题标题】:hide or remove border of wpf button in C# code在 C# 代码中隐藏或删除 wpf 按钮的边框
【发布时间】:2013-03-27 17:14:46
【问题描述】:

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

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

xaml:

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

    }

【问题讨论】:

  • 这不是创建 UI 的正确方法。您不应该在代码中创建/操作 UI 元素。发布您需要的屏幕截图,我们可以为您提供在 XAML 中实现它的正确方法。
  • 我刚刚更新了我的问题

标签: c# wpf c#-4.0 wpf-controls c#-3.0


【解决方案1】:

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

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

即

myButton.BorderThickness = new Thickness(0);

编辑:

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

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

您需要将控件的DataContext 设置为包含Schools 属性的类。如果您要动态更新学校列表,则需要实现INotifyPropertyChanged,以便 UI 知道何时更新。然后您的 Xaml 将如下所示:

<ItemsControl ItemsSource="{Binding Schools}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
             <Button Content="{Binding}" BorderThickness="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
<ItemsControl>

【讨论】:

  • button.BorderThickness = new Thickness(0) 在代码中不起作用
  • ItemsSource="Schools"....这是设置数据来源的类吗?
  • 我更新了我的答案以澄清。如果将 Thickness 设置为 0 不起作用,那么您可能需要修改按钮的 controltemplate 并移除边框:msdn.microsoft.com/en-us/library/ms753328.aspx
  • ItemsSource="Schools"....这是设置数据来源的类吗?
【解决方案2】:

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

看到这个:Removing button's border

【讨论】:

    【解决方案3】:

    快速修复:

    我必须做些什么才能有效地hide 按钮边框 - 由于按钮控制模板,我相信它利用并更改了按钮边框(即 即使您将其移除,它也会在某些触发器上绘制它我相信)...

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

    即仅设置thickness 是不够的。

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

    正确的方法:

    正确的方法 - 并且推荐 - 是编写自己的 Button 模板 - 基于微软官方的一个——或者base就可以了——然后做什么 你需要没有边框。

    对于/C#背后的代码:

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

    【讨论】:

      【解决方案4】:

      最好的方法是:

      <Style TargetType="Button">
          <Style.Resources>
              <Style TargetType="{x:Type Border}">
                  <Setter Property="CornerRadius" Value="0"/>
              </Style>
          </Style.Resources>
      </Style>
      

      【讨论】:

        【解决方案5】:

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

        这叫做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 模式。

        【讨论】:

        • 类viewmoel将在绑定中设置在哪里?
        • InitializeComponent()下方需要添加DataContext = new ViewModel()。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-24
        • 2017-09-30
        相关资源
        最近更新 更多