locked
Not able to access Border Control Border Brush Color in Windows Apps Development

    Question

  • Hello All! Greetings!

    We have a situation where we are creating Border Control Dynamically. And we are adding a Text Box Control on the Border Control to write something on the Border.
    Now we need to change the Border Brush Color from the list of combobox color.

    ((Border)ctrl).BorderBrush= tblColorComboBox.Background;
    //tblColorComboBox has list of colors.
    We are not able to get the color properly.

    To get the color, we are using the following code.
    string control_color = ((SolidColorBrush)((Border)ctrl).BorderBrush).Color.ToString();

    We are not getting the right color in control_color.
    Can someone suggest what is wrong here?

    Thank you!

    Saturday, November 15, 2014 1:29 PM

Answers

  • xaml

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions >
                <RowDefinition Height="48"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="1" x:Name="testgrid"/>
            <ComboBox Grid.Row="3" x:Name="combobox" >
                <ComboBox.ItemTemplate >
                    <DataTemplate >
                        <Border Background="{Binding Background}" Width="200" Height="24">
                            <TextBlock Text="{Binding ColorName}"/>
                        </Border>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </Grid>
    </Page>

    C#

    namespace App1
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            private Border myborder;
            public MainPage()
            {
                this.InitializeComponent();
                myborder = new Border();
                myborder.Height = 200;
                myborder.Width = 200;
                myborder.BorderThickness = new Thickness(4);
                myborder.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red);
                testgrid.Children.Add(myborder);
                combobox.ItemsSource = GetData();
                combobox.SelectionChanged += combobox_SelectionChanged;
            }
    
            private ObservableCollection<MyData> GetData()
            {
                ObservableCollection<MyData> result = new ObservableCollection<MyData>();
                result.Add(new MyData() { Background = new SolidColorBrush(Windows.UI.Colors.Red), ColorName = "Red" });
                result.Add(new MyData() { Background = new SolidColorBrush(Windows.UI.Colors.Blue), ColorName = "Blue" });
                result.Add(new MyData() { Background = new SolidColorBrush(Windows.UI.Colors.Yellow ), ColorName = "Yellow" });
    
                return result;
            }
    
            private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                myborder.BorderBrush = ((MyData)e.AddedItems[0]).Background;
            }
        }
        
        public class MyData
        {
            public Brush Background { get; set; }
            public string ColorName { get; set; }
        }
    }
    


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Sunday, November 16, 2014 5:56 PM