gpt4 book ai didi

c# - WPF 用户控件不显示

转载 作者:行者123 更新时间:2023-12-03 10:36:04 27 4
gpt4 key购买 nike

已经尝试了几个小时来了解为什么我的 UserControl 没有出现。它适用于设计器,如果我只是在本地制作一个,它就可以工作:

<local:CurrentlySelectedServerUserControl Grid.Row="1" />

接下来,我为要显示的 ViewModel 创建了一个 DataTemplate。这个 ViewModel 是 UserControl 的 DataContext。我添加了一个 ContentControl 并将其 Content 属性设置为 Binding。
<DataTemplate DataType="{x:Type local:CurrentlySelectedServerViewModel}">
<local:CurrentlySelectedServerUserControl />
</DataTemplate>

<ContentControl Content="{Binding CurrentlySelectedServer}" Grid.Row="1"/>

这里的代码是:
protected CurrentlySelectedServerViewModel _currentlySelectedServer;
public CurrentlySelectedServerViewModel CurrentlySelectedServer
{
get { return _currentlySelectedServer; }
set { _currentlySelectedServer = value; }
}

所以据我了解,这应该创建了 ContentControl,绑定(bind)到包含 CurrentSelectedServerViewModel 的属性。当它知道这一点时,它会查找 ViewModel 的 DataTemplate,并且应该绘制 UserControl。

我错过了什么吗?

我知道我的数据的初始化是有效的,因为在它上面我有一个简单的 ListView 绑定(bind)到同一个 ViewModel 的属性 TheServers。它正在列表框中打印服务器名称。

主窗口.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VpnClient"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:VpnClient_ViewModels="clr-namespace:VpnClient.ViewModels" mc:Ignorable="d"
x:Class="VpnClient.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="350" Height="480" Background="{x:Null}" Loaded="Window_Loaded">

<Window.DataContext>
<VpnClient_ViewModels:VpnClientMainWindowViewModel x:Name="VpnClientMainWindowViewModel"/>
</Window.DataContext>

<Window.Resources>
</Window.Resources>

<Grid x:Name="LayoutRoot" Background="#FF003349">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0.731*"/>
<RowDefinition Height="0.205*"/>
<RowDefinition Height="0.064*"/>
</Grid.RowDefinitions>

<Label Content="Title"
Margin="8,8,8,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="{x:Null}"
Foreground="White"
FontSize="24"/>

<Grid Grid.Row="1" Background="#FF054160">
<Grid.RowDefinitions>
<RowDefinition Height="0.726*"/>
<RowDefinition Height="0.274*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type local:CurrentlySelectedServerViewModel}">
<local:CurrentlySelectedServerUserControl />
</DataTemplate>
</Grid.Resources>
<ListBox Margin="5" ItemsSource="{Binding TheServers}" DisplayMemberPath="Name" Grid.Row="0"/>

<ContentControl
Content="{Binding Path=CurrentlySelectedServer}"
Grid.Row="1"/>

</Grid>

<Button x:Name="connectButton" Content="Connect"
Margin="10,0"
Grid.Row="2"
VerticalAlignment="Center" BorderThickness="0" Height="35">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF00FF61" Offset="0"/>
<GradientStop Color="#FF07A996" Offset="0.259"/>
<GradientStop Color="#FF006C38" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>

</Grid>

主窗口.Xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();

// Insert code required on object creation below this point.
var perthServer = new ServerModel {
Name = "Perth",
Hostname = "localhost",
Ip = "127.0.0.1",
Latency = 0
};

var sydneyServer = new ServerModel {
Name = "Sydney",
Hostname = "localhost",
Ip = "127.0.0.1",
Latency = 0
};

VpnClientMainWindowViewModel.TheServers.Add(perthServer);
VpnClientMainWindowViewModel.TheServers.Add(sydneyServer);

VpnClientMainWindowViewModel.CurrentlySelectedServer = new CurrentlySelectedServerViewModel
{
CurrentlySelectedServerModel = VpnClientMainWindowViewModel.TheServers[0]
};

}
}

VpnClientMainWindowViewModel.cs
public class VpnClientMainWindowViewModel
{

public VpnClientMainWindowViewModel() { }

protected List<ServerModel> _theServers = new List<ServerModel>();
protected CurrentlySelectedServerViewModel _currentlySelectedServer;

public List<ServerModel> TheServers
{
get { return _theServers; }
set { _theServers = value; }
}

public CurrentlySelectedServerViewModel CurrentlySelectedServer
{
get { return _currentlySelectedServer; }
set { _currentlySelectedServer = value; }
}
}

CurrentSelectedServerUserControl.xaml
<UserControl
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:local="clr-namespace:VpnClient"
mc:Ignorable="d"
x:Class="VpnClient.CurrentlySelectedServerUserControl"
x:Name="UserControl"
d:DesignWidth="350" d:DesignHeight="80" HorizontalAlignment="Center" VerticalAlignment="Center">
<UserControl.Resources>
<Style x:Key="CurrentlySelectedServerViewModelTitleStyle" TargetType="{x:Type Label}">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="#FF52AAC0"/>
</Style>
<Style x:Key="CurrentlySelectedServerViewModelDataTextStyle" TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</UserControl.Resources>
<UserControl.DataContext>
<local:CurrentlySelectedServerViewModel/>
</UserControl.DataContext>

<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0.479*"/>
</Grid.RowDefinitions>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label Content="Location:" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Column="1" Style="{DynamicResource CurrentlySelectedServerViewModelTitleStyle}"/>
<Label Content="Ping:" HorizontalAlignment="Left" Margin="0" d:LayoutOverrides="Height" Grid.Column="3" VerticalAlignment="Bottom" Style="{DynamicResource CurrentlySelectedServerViewModelTitleStyle}"/>
<Image HorizontalAlignment="Center" Height="35" Source="/VpnClient;component/Images/vpnclient_location.png" Stretch="Fill" VerticalAlignment="Center" Width="27" Grid.RowSpan="2"/>
<Image Margin="0" Source="/VpnClient;component/Images/vpnclient_ping.png" Stretch="Fill" Grid.RowSpan="2" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Height="34" Width="45"/>
<Label Content="{Binding CurrentlySelectedServerModel.Name}" Margin="0" d:LayoutOverrides="Width, Height" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Style="{DynamicResource CurrentlySelectedServerViewModelDataTextStyle}"/>
<Label Content="{Binding CurrentlySelectedServerModel.Latency}" Margin="0,0,12.006,0" d:LayoutOverrides="Width, Height" Grid.Row="2" Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Top" Style="{DynamicResource CurrentlySelectedServerViewModelDataTextStyle}"/>
</Grid>

最佳答案

这是关于绑定(bind)的。请引用以下代码。
主窗口.XAML

<Window x:Class="ComboStr_Learning.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ComboStr_Learning"
Title="Window1" Height="300" Width="300" >
<Window.DataContext>
<local:VpnClientMainWindowViewModel x:Name="VpnClientMainWindowViewModel"/>
</Window.DataContext>

<Window.Resources>
</Window.Resources>

<Grid x:Name="LayoutRoot" Background="#FF003349">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0.731*"/>
<RowDefinition Height="0.205*"/>
<RowDefinition Height="0.064*"/>
</Grid.RowDefinitions>

<Label Content="Title"
Margin="8,8,8,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="{x:Null}"
Foreground="White"
FontSize="24"/>

<Grid Grid.Row="1" Background="#FF054160">
<Grid.RowDefinitions>
<RowDefinition Height="0.726*"/>
<RowDefinition Height="0.274*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type local:CurrentlySelectedServerViewModel}">
<local:CurrentlySelectedServerUserControl />
</DataTemplate>
</Grid.Resources>
<ListBox Margin="5" ItemsSource="{Binding TheServers}" SelectedItem="{Binding CurrentlySelectedServer.CurrentlySelectedServerModel}" DisplayMemberPath="Name" Grid.Row="0"/>

<ContentControl
Content="{Binding Path=CurrentlySelectedServer}"
Grid.Row="1"/>

</Grid>

<Button x:Name="connectButton" Content="Connect"
Margin="10,0"
Grid.Row="2"
VerticalAlignment="Center" BorderThickness="0" Height="35">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF00FF61" Offset="0"/>
<GradientStop Color="#FF07A996" Offset="0.259"/>
<GradientStop Color="#FF006C38" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>

</Grid>

窗口.cs
 public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
// Insert code required on object creation below this point.
}
}

VpnClientMainWindowViewModel
public class VpnClientMainWindowViewModel
{
public VpnClientMainWindowViewModel() {

var perthServer = new ServerModel
{
Name = "Perth",
Hostname = "localhost",
Ip = "127.0.0.1",
Latency = 0
};

var sydneyServer = new ServerModel
{
Name = "Sydney",
Hostname = "localhost",
Ip = "127.0.0.1",
Latency = 0
};

this.TheServers.Add(perthServer);
this.TheServers.Add(sydneyServer);

this.CurrentlySelectedServer = new CurrentlySelectedServerViewModel
{
CurrentlySelectedServerModel = this.TheServers[0]
};
}

private List<ServerModel> _theServers = new List<ServerModel>();
private CurrentlySelectedServerViewModel _currentlySelectedServer;

public List<ServerModel> TheServers
{
get { return _theServers; }
set { _theServers = value; }
}

public CurrentlySelectedServerViewModel CurrentlySelectedServer
{
get { return _currentlySelectedServer; }
set { _currentlySelectedServer = value; }
}
}

public class CurrentlySelectedServerViewModel
{
private ServerModel _CurrentlySelectedServerModel;

public ServerModel CurrentlySelectedServerModel
{
get { return _CurrentlySelectedServerModel; }
set { _CurrentlySelectedServerModel = value; }
}
}

public class ServerModel
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

private string hostName;

public string Hostname
{
get { return hostName; }
set { hostName = value; }
}

private string ip;

public string Ip
{
get { return ip; }
set { ip = value; }
}

private int latency;

public int Latency
{
get { return latency; }
set { latency = value; }
}

}

关于c# - WPF 用户控件不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796007/

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