1

I have a stackpanel named "mystack" in my xaml file and I am adding buttons in it dynamically from the .cs file and want to remove the border of buttons in C# .cs file

what I really want is to populate this stackpanel with the buttons coming from a list of string thanks in advance

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;

    }
jacob aloysious
  • 2,547
  • 15
  • 16
user2100148
  • 25
  • 1
  • 5

5 Answers5

2

I agree with HighCore, you generally do not want to manipulate the UI elements in your code.

To remove the Border of the buttons you can set a Button's BorderThickness property to "0" in Xaml or to new Thickness(0) in the code-behind.

i.e.

myButton.BorderThickness = new Thickness(0);

EDIT:

Okay, I noticed your updated question. I would create a property that stores your list of schools and bind to it in a way similar to this:

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

Somewhere you need to set the DataContext of the control to your class containing the Schools property. If you are dynamically updating the list of Schools you'll need to implement INotifyPropertyChanged so the UI knows when to update. And then your Xaml would look something like this:

<ItemsControl ItemsSource="{Binding Schools}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
             <Button Content="{Binding}" BorderThickness="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
<ItemsControl>
Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
0

The fast Fix:

What I had to do to effectively hide the button border - and due to the button control template I believe which utilizes and changes Button border (i.e. even if you remove it it'd draw it on some trigger I believe)...

...was to set BorderBrush="Transparent" as well (I always do BorderThickness as well but I'm guessing it's not needed - only for visual/layout look'n'feel)

i.e. setting thickness alone is not enough.

I'm really not sure that's the bets way to do it, or actually I'm quite sure there must be something smarter - but I always end up with that.

The Right Way:

Proper way - and recommended - is to write your own Button template - based on the Microsoft official one - or base it on it - and do what you need w/o borders.

For the code behind/C#:

You really don't need that as per your changed question - do what others suggested already

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

You can't remove button's border like: btn.BorderThicknes=new Thickness(0);

See this: Removing button's border

Community
  • 1
  • 1
Dilshod
  • 3,189
  • 3
  • 36
  • 67
0

the best way to do this is :

<Style TargetType="Button">
    <Style.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="0"/>
        </Style>
    </Style.Resources>
</Style>
mehdi
  • 645
  • 1
  • 9
  • 9
-1

what I really want is to populate this stackpanel with the buttons coming from a list of string

That's called a ListBox:

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

ViewModel:

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

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

You need to learn the MVVM pattern.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154