在WPF铸造一个TabItem的用户控件来 [英] Casting a TabItem to UserControl in WPF
问题描述
我有我的主屏幕上的一个选项卡控件。它有不同的选项卡项目。例如:
< TabControl的名称=mainTab填充=0利润=70,80,350,49> ;
<! - 左,上,右,下 - >
< TabItem的的GotFocus =TabItem_Animals_GotFocus>
< TabItem.Header>
动物
< /TabItem.Header>
< ContentControl中保证金=10>
<框名称=animalFrame来源=AnimalWorkSpaceView.xaml>< /帧>
< / ContentControl中>
< / TabItem的>
< TabItem的的GotFocus =TabItem_Calfs_GotFocus>
< TabItem.Header>
小牛的
< /TabItem.Header>
< ContentControl中保证金=10>
<框名称=calfFrame来源=CalfWorkSpaceView.xaml>< /帧>
< / ContentControl中>
< / TabItem的>
的等.. 的
下面是标签的设计时预览:
每个选项卡项目控制是从的 WorkSpaceViewControl 的(源于一个抽象类的用户控件<继承/ EM>)
正如你可以看到有一个的刷新的按钮刷新控制(刷新它的的DataGrid 的成员)
背后的代码的刷新的按钮:
私人无效buttonRefresh_Click(对象发件人,RoutedEventArgs E)
{
//变种X = mainTab.SelectedItem作为TabItem的;
//MessageBox.Show(x.Header.ToString());//shows头
//变种T = x.Content作为TextBlock的;
//MessageBox.Show(t.Text);
VAR CTRL = mainTab.SelectedItem作为TabItem的;
VAR myCtrl1 =(WorkSpaceViewControl)CTRL;
myCtrl1.Refresh();
}
刷新()是在一个虚拟的方法的 WorkSpaceViewControl 的类,并在随后的类重写。
每当我打电话的代码,它给了我错误的铸造。我已经尝试了很多的铸造方法:隐式的,明确的(,你可以在上面看到,以及在注释代码中的一些尝试的)
这里是显式转换我试图执行(但失败了)的代码:
公共静态明确经营者WorkSpaceViewControl(TabItem的v)
{
如果(v.Content是WorkSpaceViewControl)
{
返回v.Content为WorkSpaceViewControl;
}
,否则
{
抛出新InvalidCastException的();
}
}
它总是抛出我无效的转换通过考虑其他条件
我可以投它,以及如何? 。谢谢回答它
更新
该抽象类是:
公共抽象类WorkSpaceViewControl:用户控件
{
公共WorkSpaceViewControl()
{
的InitializeComponent();
}
私人无效的InitializeComponent()
{
//
}
#地区的传承方法(子类
公共虚拟无效GetSelectedEntry()
{
}
公共虚拟无效刷新()
{
}
公共静态明确经营者WorkSpaceViewControl(TabItem的v)
{
如果(v.Content是WorkSpaceViewControl)
{
返回v.Content作为WorkSpaceViewControl;
}
,否则
{
抛出新InvalidCastException的();
}
}
#endregion
}
您有一个接口:
接口IWorkSpaceViewControl
{
无效GetSelectedEntry();
无效刷新();
布尔CanSave {获得;}
无效保存();
}
和一个用户控件:
<用户控件X:类=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:可忽略=D
D:DesignHeight =300D:DesignWidth =300>
<网格和GT;
<按钮名称=btnChangeCanSave点击=btnChangeCanSave_Click>改变CanSave< /按钮>
< /网格和GT;
< /用户控件>
代码背后:
公共部分类DemoUserControl:用户控件,IWorkSpaceViewControl
{
私人布尔canSave;
公共DemoUserControl()
{
的InitializeComponent();
}
公共无效GetSelectedEntry()
{
//你的实现
}
公共无效刷新()
{
//你的实现
的Debug.WriteLine(DemoUserControl刷新()执行);
}
公共BOOL CanSave
{
{返回canSave; }
}
私人无效btnChangeCanSave_Click(对象发件人,RoutedEventArgs E)
{
canSave = canSave!;
}
公共无效保存()
{
的Debug.WriteLine(DemoUserControl保存()执行);
}
}
和一个主窗口:
<窗口的xmlns:WpfApplication9 =CLR的命名空间:WpfApplication9X:类=WpfApplication9.MainWindow
的xmlns =HTTP:/ /schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口身高=350宽度=525>
< Window.CommandBindings>
<命令的CommandBinding =ApplicationCommands.SaveCanExecute =SaveCommand_CanExecute伏法=SaveCommand_Executed/>
< /Window.CommandBindings>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition />
< /Grid.RowDefinitions>
< WrapPanel>
<按钮名称=btnRefresh点击=btnRefresh_Click>&刷新LT; /按钮>
<按钮命令=ApplicationCommands.Save>保存< /按钮>
< / WrapPanel>
< TabControl的名称=TabControl的Grid.Row =1>
<&TabItem的GT;
< TabItem.Header>动物< /TabItem.Header>
< WpfApplication9:DemoUserControl保证金=10/>
< / TabItem的>
< / TabControl的>
< /网格和GT;
< /窗GT;
通过代码背后:
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
}
私人无效btnRefresh_Click(对象发件人,RoutedEventArgs E)
{
IWorkSpaceViewControl控制= tabControl.SelectedContent为IWorkSpaceViewControl;
control.Refresh();
}
私人无效SaveCommand_CanExecute(对象发件人,CanExecuteRoutedEventArgs E)
{
如果(TabControl的!= NULL)
{
é。 CanExecute =((IWorkSpaceViewControl)tabControl.SelectedContent).CanSave;
}
}
私人无效SaveCommand_Executed(对象发件人,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的用户控件来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!