我與Content
財產在我的ViewModel綁定到UserControl
,就像我在這個問題讀ContentControl
:WPF: how do I load user controls dynamically?但是試圖測試這個時候,我的應用程序不會運行,我收到一條消息,即Visual Studion XAML UI Designer意外崩潰,讓我毫無頭緒地知道如何以及爲何發生這種情況。ContentControl中顯示用戶控件崩潰VS XAML UI設計師
此外,在我的代碼中,您可能會注意到我使用UserControl
而不是FrameworkElement
,正如上面提到的問題中所建議的,我嘗試了兩種方法,但他們每個都給我出現了相同的錯誤。
編輯:經過一些測試,我發現了問題就出在這行代碼,但我不明白爲什麼
private UserControl _currentControl = new TableView();
EDIT2:有以上代碼應該沒有一點改進,但因爲TableView中是UserControl
,當我構建應用程序,我得到沒有錯誤
一如既往,任何幫助將不勝感激!
MainWindow.xaml
<Fluent:RibbonWindow x:Class="DatabaseExplorer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
xmlns:View="clr-namespace:DatabaseExplorer.Views"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
Height="300"
Width="300"
Title="Application"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Fluent:RibbonWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Fluent:RibbonWindow.Resources>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Fluent:Ribbon>
...
</Fluent:Ribbon>
<ContentControl Grid.Row="1" Content="{Binding CurrentControl}" />
</Grid>
</Fluent:RibbonWindow>
MainViewModel.cs
...
/// <summary>
/// The <see cref="CurrentControl" /> property's name.
/// </summary>
public const string CurrentControlPropertyName = "CurrentControl";
private UserControl _currentControl = new TableView();
/// <summary>
/// Sets and gets the CurrentControl property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public UserControl CurrentControl
{
get
{
return _currentControl;
}
set
{
if (_currentControl == value)
{
return;
}
RaisePropertyChanging(CurrentControlPropertyName);
_currentControl = value;
RaisePropertyChanged(CurrentControlPropertyName);
}
}
...
最PROBA在XAML中出現一些bug。確保你的XAML是有效的 –
@HarisHasan沒有不完全的,刪除我的視圖中的'Content'綁定和我的ViewModel中的'UserControl'屬性解決了這個問題。 – Kryptoxx