我是WPF的新手,並嘗試在選擇相應的listviewitem時動態顯示用戶控件。我已經看過了下面的問題在列表選擇上更改用戶控件
WPF: switching UserControls depending on corresponding ViewModels (MVVM)
Dynamically changing UserControl content at run time with WPF/MVVM
Dynamic user control change - WPF
所有的問題,引用MVVM,我不認爲我使用的,或者如果我是在不知不覺中。
爲了更好地解釋我正在嘗試做的事情,我在左側有一個帶有列表視圖的窗口,在右側,我想動態顯示一個用戶控件,具體取決於列表中的哪個項目被選中。
我需要添加到我的XAML以在用戶控件1和用戶控件2之間進行選擇?我需要將哪些代碼添加到「選擇操作」代碼中以更改用戶控件。
的窗口
<Window x:Class="Manager.ProfileWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Edit Profile" Height="500" Width="700">
<Grid Background="WhiteSmoke">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="150" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3*" MinWidth="300" />
</Grid.ColumnDefinitions>
<DockPanel>
<Border DockPanel.Dock="Top" Height="30" Margin="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5476F8" Offset="0"/>
<GradientStop Color="#FF001C87" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
Text=" Profile Settings" FontSize="16" FontWeight="Bold" Foreground="White" Grid.Row="1"/>
</Border>
<ListView Margin="2" x:Name="SettingsList" DockPanel.Dock="Top" ItemsSource="{Binding Settings}"></ListView>
</DockPanel>
<GridSplitter Grid.Column="1" Grid.Row="0" Width="2" HorizontalAlignment="Stretch" ResizeDirection="Columns"/>
</Grid>
</Window>
用戶控制1
<UserControl x:Class="Manager.SomeSettings"
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>
<Border VerticalAlignment="Top" Height="30" Margin="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5476F8" Offset="0"/>
<GradientStop Color="#FF001C87" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
Text="Some Settings" Padding="5,0,0,0" FontSize="16" FontWeight="Bold" Foreground="White" Grid.Row="1"/>
</Border>
</Grid>
</UserControl>
用戶控制2
<UserControl x:Class="Manager.LocationSettings"
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>
<Border VerticalAlignment="Top" Height="30" Margin="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5476F8" Offset="0"/>
<GradientStop Color="#FF001C87" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
Text="Location Settings" Padding="5,0,0,0" FontSize="16" FontWeight="Bold" Foreground="White" Grid.Row="1"/>
</Border>
</Grid>
</UserControl>
的*的.xaml。用戶控件的cs文件是空的。
不會增加新的控制對每個選擇在數據網格消耗內存爲他們之間的用戶切換?理想情況下,它應該在它們之間切換 – JME
JME,GC在調用dgRoot.Children.Clear()時應該收集不需要的對象。但是如果你不想在每個選擇中創建新的控件,請查看更改和我的其他答案。 – VMaleev