c# - WPF 用户控件选择器
问题描述
作为一个对 WPF 几乎一无所知的人......
我正在尝试切换用户控件。目前我的代码正在使用“新”来获取用户控制对象,但这非常有问题,因为这段代码被读取了很多次并且对象没有被释放,我使用的是非常弱的硬件。
有没有其他方法可以在用户控件之间切换?
我有 xaml 文件,看起来像这样
<UserControl x:Uid="UserControl_1" x:Class="ddd.WindowFooter"
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"
xmlns:local="clr-namespace:ddd"
xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:Core="clr-namespace:Microsoft.Expression.Interactivity.Core"
xmlns:ddd="clr-namespace:ddd"
xmlns:Core1="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding Source={StaticResource _indicatorSelector}}" KeyboardNavigation.TabNavigation="None">
<Border x:Uid="Border_1" HorizontalAlignment="Left" VerticalAlignment="Bottom" CornerRadius="0,10,0,0" Background="{Binding Background}">
<StackPanel x:Uid="StackPanel_1" Margin="10,10,10,0">
<Grid x:Uid="Grid_1">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Uid="ColumnDefinition_1" />
<ColumnDefinition x:Uid="ColumnDefinition_2" />
</Grid.ColumnDefinitions>
<ContentControl x:Uid="contentControl1" Content="{Binding Slot1Control}" MouseDown="DoMouseDown"
Height="40" Name="contentControl1" Width="100" Grid.Column="0" />
<ContentControl x:Uid="contentControl2" Content="{Binding Slot2Control}" MouseLeftButtonUp="DoMouseDown"
</Grid>
<Grid x:Uid="Grid_2" Grid.Row="1" Width="200" Height="22" Margin="0,7" HorizontalAlignment="Left" VerticalAlignment="Bottom">
<Viewbox x:Uid="Viewbox_1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform">
<ContentControl x:Uid="ContentControl_1" HorizontalAlignment="Left" VerticalAlignment="Top" ContentTemplate="{StaticResource SomeImage}" />
</Viewbox>
</Grid>
</StackPanel>
</Border>
</UserControl>
而 CS 看起来像
public class IndicatorSelector : INotifyPropertyChanged
{
UserControl Slot1Control{
get
{
return new Slot1UserControl();
}
}
UserControl Slot2Control{
get
{
return new Slot2UserControl();
}
}
}
谢谢
解决方案
推荐阅读
- javascript - 箭头函数不是函数。是数组的一个实例。反应原生钩子
- python - 使用 dict 映射而不是多个 if 语句
- android - 由于 Firebase UI,我的应用程序缓存增加
- java - 如何添加文本框+组合框
- qt - QTableView 和 QStandardItemModel 的问题
- java - 如何打开 Openshift - 端口 9111(grpc 应用程序)
- flutter - 如何在 Flutter 中修复文本溢出问题并使卡片变大?
- linux - 如何在 awk printf 中使用 var
- python - 在 Django Channels 中创建等待队列?
- r - 如何在这个由几个不同几何图形组成的 ggplot 中手动指定图例文本/颜色?