Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

how to set a border content in code behind

$
0
0

hi everybody.

I am making a usercontrol in which I have a border. I want to use it as a container.

to be specific I am making something like a window control. I have the title bar and I added a border which contains everything in the window.

here is my xaml.

<UserControl x:Class="ArianControl.Controls.CustomWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
              d:DesignWidth="500"
	  Background="Transparent" FlowDirection="RightToLeft" MouseDoubleClick="UserControl_MouseDoubleClick_1" MouseDown="UserControl_MouseDown_1"><!--  --><UserControl.Resources><!-- Window style --><!-- Window show animation --><Storyboard  x:Key="Bordershow"><DoubleAnimation Storyboard.TargetProperty="Width" From="0" To="{Binding Path=Width}" Duration="0:0:0.5" /><DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="{Binding Path=Height}" Duration="0:0:0.5" /></Storyboard><Storyboard  x:Key="Windowshow"><DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.5" /></Storyboard><!-- Window close animation --><Storyboard  x:Key="Windowclose"><DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.5" Completed="CloseAnimation_Completed"/></Storyboard><!-- Window maximize animation --><!--maximize--><Storyboard  x:Key="Maximized1"><DoubleAnimation Storyboard.TargetProperty="Opacity"  From="1" To="0" Duration="0:0:0.5" Completed="MaximizedAnimation_Half"/></Storyboard><Storyboard  x:Key="Maximized2"><DoubleAnimation Storyboard.TargetProperty="Opacity"  From="0" To="1" Duration="0:0:0.5" /></Storyboard><!--Normal--><Storyboard  x:Key="Maximized3"><DoubleAnimation Storyboard.TargetProperty="Opacity"  From="1" To="0" Duration="0:0:0.5" Completed="MaximizedAnimation_Half1"/></Storyboard><!-- Window minimze animation --><!--Minimize<Storyboard  x:Key="Minimize"><DoubleAnimation Storyboard.TargetProperty="Height"  From="{Binding Path=Height}" To="0" Duration="0:0:0.5" /><DoubleAnimation Storyboard.TargetProperty="Width"  From="{Binding Path=Width}" To="0" Duration="0:0:0.5" Completed="Minimize_Half"/></Storyboard>--><!--Normal<Storyboard  x:Key="Minimize2"><DoubleAnimation Storyboard.TargetProperty="Height" To="{Binding Path=Height}" Duration="0:0:0.5"/><DoubleAnimation Storyboard.TargetProperty="Width" To="{Binding Path=Width}" Duration="0:0:0.5" Completed="Minimize_Completed"/></Storyboard>--><!-- Border --><Style TargetType="Border"><Setter Property="BorderThickness" Value="2"/><Setter Property="BorderBrush" Value="{DynamicResource Border}"/></Style><!-- Titlebar button --><Style TargetType="Button"><Setter Property="Width" Value="25"/><Setter Property="Height" Value="25"/><Setter Property="Background" Value="Transparent"/><Setter Property="BorderThickness" Value="0"/><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Blue"/></Trigger></Style.Triggers></Style><!-- Title bar Grid--><Style TargetType="Grid" x:Key="Title"><Setter Property="Background" Value="#FF179EDC"/><Setter Property="VerticalAlignment" Value="Top"/><Setter Property="Height" Value="30"/></Style><!-- TextBox stackpanel --><Style TargetType="TextBox"><Setter Property="Background" Value="#FF179EDC"/><Setter Property="Width" Value="190"/><Setter Property="BorderBrush" Value="#FF0F81B4"/><Style.Triggers><Trigger Property="IsFocused" Value="True"><Setter Property="Background" Value="White"/><Setter Property="Width" Value="200"/><Setter Property="BorderThickness" Value="3"/></Trigger><EventTrigger RoutedEvent="UIElement.GotFocus"><BeginStoryboard><Storyboard><DoubleAnimation Storyboard.TargetProperty="Width" 
                                             From="190" By="10" Duration="0:0:0.2" /></Storyboard></BeginStoryboard></EventTrigger><EventTrigger RoutedEvent="UIElement.LostFocus"><BeginStoryboard><Storyboard><DoubleAnimation Storyboard.TargetProperty="Width" 
                                             From="200" By="-10" Duration="0:0:0.2" /></Storyboard></BeginStoryboard></EventTrigger></Style.Triggers></Style><!-- Local resources --><SolidColorBrush x:Key="WindowIconsBrush" Color="#FFECECEC"/></UserControl.Resources><DockPanel x:Name="Dock" Loaded="Dock_Loaded" ><Grid  Style="{DynamicResource Title}" DockPanel.Dock="Top" x:Name="TitleGrid"><Grid.ColumnDefinitions><ColumnDefinition Width="30"/><ColumnDefinition Width="30"/><ColumnDefinition Width="30"/><ColumnDefinition Width="*"/><ColumnDefinition Width="100"/><ColumnDefinition Width="*"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><Button x:Name="btnClose"  Grid.Column="0" Click="btnClose_Click" ><Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  ><Grid><Grid Name="CloseIcon" Width="25" Height="25" Visibility="Collapsed" /><Path Data="F1M54.0573,47.8776L38.1771,31.9974 54.0547,16.1198C55.7604,14.4141 55.7604,11.6511 54.0573,9.94531 52.3516,8.23962 49.5859,8.23962 47.8802,9.94531L32.0026,25.8229 16.1224,9.94531C14.4167,8.23962 11.6511,8.23962 9.94794,9.94531 8.24219,11.6511 8.24219,14.4141 9.94794,16.1198L25.8255,32 9.94794,47.8776C8.24219,49.5834 8.24219,52.3477 9.94794,54.0534 11.6511,55.7572 14.4167,55.7585 16.1224,54.0534L32.0026,38.1745 47.8802,54.0534C49.5859,55.7585 52.3516,55.7572 54.0573,54.0534 55.7604,52.3477 55.763,49.5834 54.0573,47.8776z" Stretch="Uniform" Fill="{DynamicResource WindowIconsBrush}" Width="23" Height="23" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"><Path.RenderTransform><TransformGroup><TransformGroup.Children><RotateTransform Angle="0" /><ScaleTransform ScaleX="1" ScaleY="1" /></TransformGroup.Children></TransformGroup></Path.RenderTransform></Path></Grid></Viewbox></Button><Button x:Name="btnMaximise"  Grid.Column="1" Click="btnMaximise_Click" ><Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Grid><Grid Name="MaximizeIcon" Width="25" Height="25" Visibility="Collapsed" /><Path  Data="M119.9996,0L187.33301,20.667028 187.33301,206.66699 119.33293,226.66699 0,182.00028 120.667,197.77798 120.667,36.444982 40.222468,55.555956 40.222468,165.33381 0,181.99999 0,46.000032z"
                               Stretch="Uniform" Fill="{DynamicResource WindowIconsBrush}" 
                               Width="23" Height="23" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5" ><Path.RenderTransform><TransformGroup><TransformGroup.Children><RotateTransform Angle="0" /><ScaleTransform ScaleX="1" ScaleY="1" /></TransformGroup.Children></TransformGroup></Path.RenderTransform></Path></Grid></Viewbox></Button><Button x:Name="Minimise" Grid.Column="2" Click="Minimise_Click" ><Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Grid x:Name="btnMnimaze" ><Grid Name="MinimizedIcon" Width="16" Height="16" Visibility="Collapsed" /><Path Data="M0,0L53.333,0 53.333,8.888 0,8.888z" Stretch="Uniform" Fill="{DynamicResource WindowIconsBrush}" Width="16" Height="16" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"><Path.RenderTransform><TransformGroup><TransformGroup.Children><RotateTransform Angle="0" /><ScaleTransform ScaleX="1" ScaleY="1" /></TransformGroup.Children></TransformGroup></Path.RenderTransform></Path></Grid></Viewbox></Button><Label x:Name="ApplicationTitle" Grid.Column="5"/><TextBox x:Name="TXTSearch" Grid.Column="7" Text="search..." GotFocus="TXTSearch_GotFocus" LostFocus="TXTSearch_LostFocus" /></Grid><Border DockPanel.Dock="Top" x:Name="HolderBorder" /></DockPanel></UserControl>

and here is the code behind

using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media.Animation;
namespace AmozeshGah.Controls
{
    /// <summary>
    /// Interaction logic for CustomWindow.xaml
    /// </summary>
    /// 
    [ContentProperty("HolderControl")]
    public partial class CustomWindow : UserControl
    {
        private bool State = false;
        public CustomWindow()
        {
            InitializeComponent();
        }

        public virtual UIElement HolderControl
        {
            get
            {
                return HolderBorder.Child;
            }
            set
            {
                HolderBorder.Child = value;
            }
        }
        private void TXTSearch_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            TXTSearch.Text = "";
        }

        private void TXTSearch_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (TXTSearch.Text.Length == 0)
                TXTSearch.Text = "search...";

        }
        public string Title
        {
            get
            {
                return "title";
            }
        }
        public string BorderContent
        {
            get
            {
                return Content.ToString();
            }
        }

        #region windowClose
        //callin an animation to close the window
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {

            Window parent = Window.GetWindow(this);
            Storyboard mystory = (Storyboard)this.FindResource("Windowclose");
            mystory.Begin(parent);

        }

        private void CloseAnimation_Completed(object sender, System.EventArgs e)
        {
            Window parent = Window.GetWindow(this);
            parent.Close();
        }
        #endregion
        #region windowstatechange
        // calling animations to maximize and normal window's state
        private void btnMaximise_Click(object sender, RoutedEventArgs e)
        {
            Window parent = Window.GetWindow(this);
            if (!State)
            {
                Storyboard mystory = (Storyboard)this.FindResource("Maximized1");
                mystory.Begin(parent);


            }
            else
            {
                Storyboard mystory = (Storyboard)this.FindResource("Maximized3");
                mystory.Begin(parent);

            }

        }

        private void UserControl_MouseDoubleClick_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Window parent = Window.GetWindow(this);
            if (!State)
            {
                Storyboard mystory = (Storyboard)this.FindResource("Maximized1");
                mystory.Begin(parent);
            }
            else
            {
                Storyboard mystory = (Storyboard)this.FindResource("Maximized3");
                mystory.Begin(parent);
            }
        }
        //Maximize window's state
        private void MaximizedAnimation_Half(object sender, System.EventArgs e)
        {
            Window parent = Window.GetWindow(this);
            parent.WindowState = WindowState.Maximized;
            State = true;
            Dock.Height = parent.Height;
            Dock.Width = parent.Width;
            HolderBorder.Width = Dock.Width;
            HolderBorder.Height = Dock.Height - 35;
           Storyboard mystory = (Storyboard)this.FindResource("Maximized2");
            mystory.Begin(parent);

        }
        //Normal window's state
        private void MaximizedAnimation_Half1(object sender, System.EventArgs e)
        {
            Window parent = Window.GetWindow(this);
            parent.WindowState = WindowState.Normal;
            State = false;
            Dock.Height = parent.Height;
            Dock.Width = parent.Width;
            HolderBorder.Width = Dock.Width;
            HolderBorder.Height = Dock.Height - 35;
            Storyboard mystory = (Storyboard)this.FindResource("Maximized2");
            mystory.Begin(parent);

        }
        #endregion
        #region MinimizeWindow
      
        private void Minimise_Click(object sender, RoutedEventArgs e)
        {
            Window parent = Window.GetWindow(this);
            parent.WindowState = WindowState.Minimized;

        }
        #endregion
        #region windowsload
        //load event for window
        private void Dock_Loaded(object sender, RoutedEventArgs e)
        {
            Window parent = Window.GetWindow(this);
            Dock.Height = parent.Height;
            Dock.Width = parent.Width;
            HolderBorder.Width = Dock.Width;
            HolderBorder.Height = Dock.Height - 35;
            Storyboard mystory = (Storyboard)this.FindResource("Bordershow");
            mystory.Begin(HolderBorder);
            Storyboard mystory1 = (Storyboard)this.FindResource("Windowshow");
            mystory1.Begin(Dock);

        }
        #endregion


        public Visibility Search
        {
            get
            {
                return this.TXTSearch.Visibility;
            }
            set
            {
                this.TXTSearch.Visibility = value;
            }
        }


        private void UserControl_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            App.Current.MainWindow.DragMove();
        }



    }
}

ok so far so good.

when I use it in a window I act's fine but when I use x:name tag for any element inside it I face an error:

Error 1 Cannot set Name attribute value 'CMBCours' on element 'ComboBox'. 'ComboBox' is under

the scope of element 'CustomWindow', which already had a name registered when it was defined in

another scope. Line 39 Position 60. G:\Project\WpfCustomizedControls\WpfCustomizedControl\MainWindow.xaml 39 60 WpfCustomizedControl

what is it? and how should I deal with it?

many thanks.

Arian


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>