gpt4 book ai didi

c# - WPF,有条件地将项目绑定(bind)到详细 View

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

我有以下代码,我花了很长时间才修复,有人能帮帮我吗

                   //contains list of client computers 
//I want to show the detail of a user in the
selected client

<ListBox x:Name="clientsListBox"
Margin="0,0,772,27"
ItemTemplate="{StaticResource clientTemplete}" />

// i did the following
<Grid Name="UserDetailGrid"
Width="414"
Height="336"
Margin="566,13,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
DataContext="{Binding SelectedItem,
ElementName=clientsListBox}"

>


<Image Margin="156,10,193,262" Source="/EtimerServer;component/rec/user.png" />
<Label Width="196"
Height="41"
Margin="89,84,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{Binding CurrentUser.Name}" />

<Label Width="280"
Height="54"
Margin="56,260,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{Binding CurrentUser.StartSession}" />
<Label Width="280"
Height="66"
Margin="56,161,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{Binding CurrentUser.ElapsedTime}"
FontFamily="Open 24 Display St"
FontSize="48"
FontWeight="Normal" />

</Grid>

这很好用,但我有两种用户 GUEST 和 customer 它们具有不同的属性

我要绑定(bind)选定的客户端CURRENT USER条件 如果用户是其他我的访客模板的访客并且用户是客户
输入我的客户模板!

我有以下模板

         <!--  Customer templete  -->
<DataTemplate x:Key="customerTemplete" DataType="{x:Type local:Customer}">

<StackPanel Margin="4" Orientation="Horizontal">

<Image Source="/EtimerServer;component/rec/user.png" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Name}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding StartSession}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Elapsed}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Balance}"
TextAlignment="Center" />


</StackPanel>

</DataTemplate>

<!-- guest templete -->
<DataTemplate x:Key="guestTemplete" DataType="{x:Type local:Guest}">

<StackPanel Margin="4" Orientation="Horizontal">

<Image Source="/EtimerServer;component/rec/user.png" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Name}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding StartSession}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Elapsed}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding TotalPayment}"
TextAlignment="Center" />


</StackPanel>

</DataTemplate>

最佳答案

您应该将 ContentControl 绑定(bind)到 ViewModel 中的 CurrentUser 属性。然后根据您放置在那里的类型,将选择正确的模板。您的实体需要从相同的基类派生:

 public abstract class BaseEntity
{

}

public class Customer : BaseEntity
{
}

public class Guest:BaseEntity
{
}

你的 View 模型:

public class ViewModel : INotifyPropertyChanged
{
private BaseEntity _currentUser;
public BaseEntity CurrentUser
{
get { return _currentUser; }
set { _currentUser = value; OnPropertyChanged();}
}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}

和你的观点:

<Grid>
<Grid.Resources>
<DataTemplate x:Key="customerTemplete" DataType="{x:Type local:Customer}">

<StackPanel Margin="4" Orientation="Horizontal">

<Image Source="/EtimerServer;component/rec/user.png" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Name}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding StartSession}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Elapsed}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Balance}"
TextAlignment="Center" />


</StackPanel>

</DataTemplate>
<!-- guest templete -->
<DataTemplate x:Key="guestTemplete" DataType="{x:Type local:Guest}">

<StackPanel Margin="4" Orientation="Horizontal">

<Image Source="/EtimerServer;component/rec/user.png" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Name}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding StartSession}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding Elapsed}"
TextAlignment="Center" />

<TextBlock FontSize="20"
Foreground="Blue"
Text="{Binding TotalPayment}"
TextAlignment="Center" />


</StackPanel>

</DataTemplate>
</Grid.Resources>
<ContentControl Content="{Binding CurrentUser}"></ContentControl>
</Grid>

关于c# - WPF,有条件地将项目绑定(bind)到详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205930/

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