gpt4 book ai didi

c# - 在 WPF 中将 TabItem 转换为 UserControl

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:16 25 4
gpt4 key购买 nike

我的主屏幕上有一个选项卡控件。它有不同的选项卡项。例如:

<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>

等等..

这是选项卡的设计时预览:

Tab Controls overview

每个选项卡项控件都继承自 WorkSpaceViewControl(派生自 UserControl 的抽象类)

如您所见,有一个Refresh 按钮来刷新控件(重新加载它的datagrid 成员)

刷新按钮背后的代码是:

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() 是 WorkSpaceViewControl 类中的虚方法,并在后续类中被覆盖。

每当我调用该代码时,它都会给我转换错误。我尝试了很多转换方法:隐式、显式(您也可以在上面的注释代码中看到一些尝试)。

这是我尝试实现(但失败)的显式转换代码:

public static explicit operator WorkSpaceViewControl(TabItem v)
{
if (v.Content is WorkSpaceViewControl)
{
return v.Content as WorkSpaceViewControl;
}
else
{
throw new InvalidCastException();
}
}

它总是通过考虑 else 条件抛出我无效的转换:

Invalid Cast

我可以施放它吗?如何施放?感谢您的回答。

更新

抽象类是:

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
}

最佳答案

你有一个界面:

interface IWorkSpaceViewControl
{
void GetSelectedEntry();
void Refresh();

bool CanSave { get; }
void Save();
}

还有一个用户控件:

<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>

代码隐藏:

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");
}
}

和一个主窗口:

<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>

代码隐藏:

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();
}
}

关于c# - 在 WPF 中将 TabItem 转换为 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com