disabling the controls located in the sub page inherited from a base page
hi,
if i have a base page which contents a button called baseButton. I also have a few sub pages which inherits form this base page class. how can i enable/disable the other buttons,grid,stackpanel or other controls which located in the sub page depending on the status IsCheck of the baseButton? Can anyone give me a simple example or suggestion of doing this the most effective way?
Thanks in advance
Answers
- Deriving from XAML generated base class is not supported in current version of WPF, so basically the type of scenario you are looking for is not supported, you'd better choose another approach instead.
Actually you can re-template the Page, and change its control template to include the type of elements you want to put on the base page as follows:
Code Blockpublic class PageBase : Page
{
static PageBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PageBase), new FrameworkPropertyMetadata(typeof(PageBase)));
}
}Themes/Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:WpfControlLib">
<Style TargetType="{x:Type cc:PageBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type cc:PageBase}">
<Border Background="{TemplateBinding Panel.Background}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Content="Enable Content" Name="PART_CheckBox"/>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
Grid.Row="1"
IsEnabled="{Binding Path=IsChecked, ElementName=PART_CheckBox}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Then you can derived Page, you can write something like the following:
Code Block<cc:PageBase x:Class="AnswerHarness.ButtonPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:WpfControlLib;assembly=WpfControlLib"
Title="ButtonPage">
<StackPanel Name="stack" VerticalAlignment="Center" HorizontalAlignment="Center">
<ListView>
<ListViewItem Content="Item 1"/>
<ListViewItem Content="Item 2"/>
<ListViewItem Content="Item 3"/>
</ListView>
<Button Content="Item 4"/>
<Button Content="Item 5"/>
</StackPanel>
</cc:PageBase>
Hope this helps
All Replies
<Grid>
<CheckBox Name="check"/>
<StackPanel Name="stack" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="{Binding ElementName=check, Path=IsChecked}">
<ListView>
<ListViewItem Content="Item 1"/>
<ListViewItem Content="Item 2"/>
<ListViewItem Content="Item 3"/>
</ListView>
<Button Content="Item 4"/>
<Button Content="Item 5"/>
</StackPanel>
</Grid>This disables the whole StackPanel (which you may look as your subpage) and is content according to the CheckBox (which you may look as your basebutton on the main page) status.
If you want only to disable the controls you may binding each control to the status of the CheckBox (or basebutton).
sorry, i should have been more clear. When i mention about the sub page inherits from the base page i meant like below:
in C#:
public partial class MyBasePage : System.Windows.Controls.Page
{
......}
public partial class SubPage :MyBasePage
{.............
So how can i ensure the controls in subpage is disabled when a toggle button in the base page is not checked?Thanks in advance
- Deriving from XAML generated base class is not supported in current version of WPF, so basically the type of scenario you are looking for is not supported, you'd better choose another approach instead.
Actually you can re-template the Page, and change its control template to include the type of elements you want to put on the base page as follows:
Code Blockpublic class PageBase : Page
{
static PageBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PageBase), new FrameworkPropertyMetadata(typeof(PageBase)));
}
}Themes/Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:WpfControlLib">
<Style TargetType="{x:Type cc:PageBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type cc:PageBase}">
<Border Background="{TemplateBinding Panel.Background}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Content="Enable Content" Name="PART_CheckBox"/>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
Grid.Row="1"
IsEnabled="{Binding Path=IsChecked, ElementName=PART_CheckBox}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Then you can derived Page, you can write something like the following:
Code Block<cc:PageBase x:Class="AnswerHarness.ButtonPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:WpfControlLib;assembly=WpfControlLib"
Title="ButtonPage">
<StackPanel Name="stack" VerticalAlignment="Center" HorizontalAlignment="Center">
<ListView>
<ListViewItem Content="Item 1"/>
<ListViewItem Content="Item 2"/>
<ListViewItem Content="Item 3"/>
</ListView>
<Button Content="Item 4"/>
<Button Content="Item 5"/>
</StackPanel>
</cc:PageBase>
Hope this helps


