WPF: How to access a control in style
Source:Open Clip Art Using Under Public Domain Attribution
I got a control in style template at one of my custom controls, Like this:
<Style TargetType="{x:Type advgenControls:CollapsibleControl}" x:Key="LeftPanel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type advgenControls:CollapsibleControl}">
<Grid>
<ContentPresenter x:Name="PART_ContentPresenter"/>
<ResizeGrip Grid.Row="1" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" HorizontalContentAlignment="Right" Margin="0,0,2,2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I just want to call that "PART_ContentPresenter". But that is xaml living in resources.
That is easy I can use GetTemplateChild("PART_ContentPresenter" ) and call at the OnApplyTemplate(). Please remember don't use it at OnInitialized(EventArgs e). At that time, styles has not loaded yet, you get a null object.:
Code
public override void OnApplyTemplate() | |
{ | |
base.OnApplyTemplate(); | |
if (this.IsInitialized) | |
{ | |
ContentPresenter cp = GetTemplateChild("PART_ContentPresenter" ) as ContentPresenter; | |
if(sections.Count >0){ | |
cp.Content = sections[0]; | |
} | |
} | |
} |
WPF Error:cannot set name attribute value '{control}'
Source:Open Clip Art Using Under Public Domain Attribution
I found the wpf user control cannot have any child has a name attribute, such as:
<advgenControls:CollapsibleControl> <TreeView x:Name="treeView" Style="{StaticResource MainLeftPanel}" /></advgenControls:CollapsibleControl>
It will have the error like this:
Cannot set Name attribute value 'treeView' on element 'TreeView'. 'TreeView' is under the scope of element 'CollapsibleControl', which already had a name registered when it was defined in another scope. Line 93 Position 5. (MC3093) - D:\Projects\AdvGenContact\AdvGenContact\AdvGenContact.WPF\MainWindow.xaml:93,5
I did a research on internet, I even tried some suggestions in internet, it doesn't work at all. I found that is no way to do that. I believe that is because User Control is not for that purpose. The user control is to hold some repeatable content across the applications. For example, I got a status bar which it need to place all every window in my application. Then I should create a user control for my own status bar and place into every window. In my case mentioned above, I wish to build a collapsible panel which has a different content in each window, even I will reuse that in other project. User Control is not my best. I should find an existing control to override. In this case, I should use HeaderItemsControl.
Note that: when I finish this control, I will put on my open source probject, AdvGen Contact Manager.
Horizontal Stackpanel in WPF
Stackpanel is a useful layout control in WPF. If you have a set of elements need to sit side by side each other, this is your best option. For example, in my advgen contact manager, in the right hand side, there is a set of buttons. Thus, I placed a stack panel. However, by the default, the controls inside the panel will flow in vertical direct, you need to set Orientation="Horizontal"
E.g.
<StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
Styles in WPF
I have some styling in my WPF opensource project, AdvGen Contact Manager. That is similar with css, just state the class name like :
<Grid
Grid.Column="1"
Name="ContentPanel"
Style="{StaticResource BluePanel}">
Running SQL Backup in .Net
If you are using SQL Express, you have sql agent to do the backup for you. The only way is using T-SQL in a application code to do that.
I have a C# library to do and winfrom application to do the scheduled task as the example, click here to buy it. That is only AUD$3! It can save you few hours of programming, That is very easy, just like the example below
Code
AdvGenSQLBackup backup = new AdvGenSQLBackup(); | |
backup.DBName = txtDBName.Text; | |
backup.Server = txtServerName.Text; | |
backup.UserName = txtUserName.Text; | |
backup.Password = txtPassword.Text; | |
backup.File = txtFile.Text; | |
backup.Run(); |
Moreover, you can use it in your web application. Then your web application can generate a backup file to your users for downloading.That is very useful.