在WPF中将TabItem转换为UserControl [英] Casting a TabItem to UserControl in WPF
问题描述
我在主屏幕上有一个Tab控件。它有不同的选项卡项。例如:
< TabControl Name =mainTabPadding =0Margin =70,80,350,49> ;
<! - Left,Top,Right,Bottom - >
< TabItem GotFocus =TabItem_Animals_GotFocus>
< TabItem.Header>
动物
< /TabItem.Header>
< ContentControl Margin =10>
< Frame Name =animalFrameSource =AnimalWorkSpaceView.xaml>< / Frame>
< / ContentControl>
< / TabItem>
< TabItem GotFocus =TabItem_Calfs_GotFocus>
< TabItem.Header>
Calfs
< /TabItem.Header>
< ContentControl Margin =10>
< Frame Name =calfFrameSource =CalfWorkSpaceView.xaml>< / Frame>
< / ContentControl>
< / TabItem>
等等。
以下是标签的设计时预览:
每个选项卡项控件都继承自 WorkSpaceViewControl (一个派生自 UserControl )
正如你所看到的,有一个刷新按钮来刷新控件(重载 datagrid
按钮的代码是:private void buttonRefresh_Click(object sender,RoutedEventArgs e)
{
// var x = mainTab.SelectedItem as TabItem;
//MessageBox.Show(x.Header.ToString());//显示标题
// var t = x.Content as TextBlock;
//MessageBox.Show (t.Text);
var ctrl = mainTab.SelectedItem as TabItem;
var myCtrl1 =(WorkSpaceViewControl)ctrl;
myCtrl1.Refresh();
}
Refresh()是WorkSpaceViewControl中的一个虚拟方法
每当我调用该代码时,它在转换时会出现错误。我已经尝试了很多方法来投射:隐式,显式(,你可以看到一些尝试在上面的注释代码以及)。
这里是显式转换的代码我试图实现(但失败):
public static explicit operator WorkSpaceViewControl(TabItem v)
{
if(v.Content是WorkSpaceViewControl)
{
return v.Content as WorkSpaceViewControl;
}
else
{
throw new InvalidCastException();
}
}
它总是通过接受else条件:
我可以投放它吗?如何?非常感谢您回答。
UPDATE
抽象类是: p>
public abstract class WorkSpaceViewControl:UserControl
{
public WorkSpaceViewControl()
{
InitializeComponent();
}
private void InitializeComponent()
{
//
}
#region继承方法
public virtual void GetSelectedEntry()
{
}
public virtual void Refresh()
{
$ b b
public static explicit operator WorkSpaceViewControl(TabItem v)
{
if(v.Content是WorkSpaceViewControl)
{
return v.Content as WorkSpaceViewControl;
}
else
{
throw new InvalidCastException();
}
}
b $ b #endregion
}
有一个接口:
interface IWorkSpaceViewControl
{
void GetSelectedEntry();
void Refresh ();
bool CanSave {get;}
void Save();
}
和一个userControl:
< UserControl x:Class =WpfApplication9.DemoUserControl
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:DesignHeight =300d:DesignWidth =300>
< Grid>
< Button Name =btnChangeCanSaveClick =btnChangeCanSave_Click>更改CanSave< / Button>
< / Grid>
< / UserControl>
代码背后:
public partial class DemoUserControl:UserControl,IWorkSpaceViewControl
{
private bool canSave;
public DemoUserControl()
{
InitializeComponent();
}
public void GetSelectedEntry()
{
//您的实现
}
public void Refresh()
{
//您的实现
Debug.WriteLine(DemoUserControl Refresh()executed);
}
public bool CanSave
{
get {return canSave; }
}
private void btnChangeCanSave_Click(object sender,RoutedEventArgs e)
{
canSave =!canSave;
}
public void Save()
{
Debug.WriteLine(DemoUserControl Save()executed);
}
}
和一个MainWindow:
< Window xmlns:WpfApplication9 =clr-namespace:WpfApplication9x:Class =WpfApplication9.MainWindow
xmlns =http: /schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题=MainWindow高度=350Width =525>
< Window.CommandBindings>
< CommandBinding Command =ApplicationCommands.SaveCanExecute =SaveCommand_CanExecuteExecuted =SaveCommand_Executed/>
< /Window.CommandBindings>
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =auto/>
< RowDefinition />
< /Grid.RowDefinitions>
< WrapPanel>
< Button Name =btnRefreshClick =btnRefresh_Click> Refresh< / Button>
< Button Command =ApplicationCommands.Save> Save< / Button>
< / WrapPanel>
< TabControl Name =tabControlGrid.Row =1>
< TabItem>
< TabItem.Header>动物< /TabItem.Header>
< WpfApplication9:DemoUserControl Margin =10/>
< / TabItem>
< / TabControl>
< / Grid>
< / Window>
代码背后:
public partial MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnRefresh_Click(object sender,RoutedEventArgs e)
{
IWorkSpaceViewControl control = tabControl.SelectedContent as IWorkSpaceViewControl;
control.Refresh();
}
private void SaveCommand_CanExecute(object sender,CanExecuteRoutedEventArgs e)
{
if(tabControl!= null)
{
CanExecute =((IWorkSpaceViewControl)tabControl.SelectedContent).CanSave;
}
}
private void SaveCommand_Executed(object sender,ExecutedRoutedEventArgs e)
{
((IWorkSpaceViewControl)tabControl.SelectedContent).Save
}
}
I have a Tab Control on my main screen. It has different tab items. For example:
<TabControl Name="mainTab" Padding="0" Margin="70,80,350,49">
<!--Left,Top,Right, Bottom-->
<TabItem GotFocus="TabItem_Animals_GotFocus">
<TabItem.Header>
Animals
</TabItem.Header>
<ContentControl Margin="10">
<Frame Name="animalFrame" Source="AnimalWorkSpaceView.xaml"></Frame>
</ContentControl>
</TabItem>
<TabItem GotFocus="TabItem_Calfs_GotFocus">
<TabItem.Header>
Calfs
</TabItem.Header>
<ContentControl Margin="10">
<Frame Name="calfFrame" Source="CalfWorkSpaceView.xaml"></Frame>
</ContentControl>
</TabItem>
and so on..
Here is design-time preview of tabs:
Every tab item control is inherited from WorkSpaceViewControl (an abstract class derived from UserControl)
As you can see there is a Refresh button to refresh the control (Reload it's datagrid members)
The code behind Refresh button is:
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
//var x = mainTab.SelectedItem as TabItem;
//MessageBox.Show(x.Header.ToString());//shows the header
//var t = x.Content as TextBlock;
//MessageBox.Show(t.Text);
var ctrl = mainTab.SelectedItem as TabItem;
var myCtrl1 = (WorkSpaceViewControl)ctrl;
myCtrl1.Refresh();
}
Refresh() is a virtual method in WorkSpaceViewControl class and overridden in subsequent classes.
Whenever I call that code, it gives me error on casting. I have tried a lot of methods of casting: Implicit, explicit (as you can see some tries in commented code above as well).
Here is code of Explicit casting I tried to implement (but failed):
public static explicit operator WorkSpaceViewControl(TabItem v)
{
if (v.Content is WorkSpaceViewControl)
{
return v.Content as WorkSpaceViewControl;
}
else
{
throw new InvalidCastException();
}
}
It always throws me Invalid Cast by taking into else condition:
Can I cast it and How? Thanks for answering it.
UPDATE
The abstract class is:
public abstract class WorkSpaceViewControl : UserControl
{
public WorkSpaceViewControl()
{
InitializeComponent();
}
private void InitializeComponent()
{
//
}
#region Inheritance Methods (for sub classes
public virtual void GetSelectedEntry()
{
}
public virtual void Refresh()
{
}
public static explicit operator WorkSpaceViewControl(TabItem v)
{
if (v.Content is WorkSpaceViewControl)
{
return v.Content as WorkSpaceViewControl;
}
else
{
throw new InvalidCastException();
}
}
#endregion
}
You have an interface:
interface IWorkSpaceViewControl
{
void GetSelectedEntry();
void Refresh();
bool CanSave { get; }
void Save();
}
And a userControl:
<UserControl x:Class="WpfApplication9.DemoUserControl"
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:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Name="btnChangeCanSave" Click="btnChangeCanSave_Click">Change CanSave</Button>
</Grid>
</UserControl>
Code Behind:
public partial class DemoUserControl : UserControl, IWorkSpaceViewControl
{
private bool canSave;
public DemoUserControl()
{
InitializeComponent();
}
public void GetSelectedEntry()
{
// Your implementation
}
public void Refresh()
{
// Your Implementation
Debug.WriteLine("DemoUserControl Refresh() executed");
}
public bool CanSave
{
get { return canSave; }
}
private void btnChangeCanSave_Click(object sender, RoutedEventArgs e)
{
canSave = !canSave;
}
public void Save()
{
Debug.WriteLine("DemoUserControl Save() executed");
}
}
and a MainWindow:
<Window xmlns:WpfApplication9="clr-namespace:WpfApplication9" x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Save" CanExecute="SaveCommand_CanExecute" Executed="SaveCommand_Executed"/>
</Window.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<WrapPanel>
<Button Name="btnRefresh" Click="btnRefresh_Click">Refresh</Button>
<Button Command="ApplicationCommands.Save">Save</Button>
</WrapPanel>
<TabControl Name="tabControl" Grid.Row="1">
<TabItem>
<TabItem.Header>Animals</TabItem.Header>
<WpfApplication9:DemoUserControl Margin="10" />
</TabItem>
</TabControl>
</Grid>
</Window>
with Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnRefresh_Click(object sender, RoutedEventArgs e)
{
IWorkSpaceViewControl control = tabControl.SelectedContent as IWorkSpaceViewControl;
control.Refresh();
}
private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (tabControl != null)
{
e.CanExecute = ((IWorkSpaceViewControl)tabControl.SelectedContent).CanSave;
}
}
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
((IWorkSpaceViewControl)tabControl.SelectedContent).Save();
}
}
这篇关于在WPF中将TabItem转换为UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!