C# WPF,有条件地将项目绑定到详图视图
我有下面的代码,它花了我一辈子的时间来修复,有人能帮我吗C# WPF,有条件地将项目绑定到详图视图,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我有下面的代码,它花了我一辈子的时间来修复,有人能帮我吗 //contains list of client computers //I want to show the detail of a user in the selected client <ListBox x:Name="clientsListBox"
//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>
//包含客户端计算机的列表
//我想在中显示用户的详细信息
选定客户
//我做了以下几件事
这很好,但我有两种用户-来宾和客户
它们有不同的性质
我想绑定所选客户端当前用户条件
如果用户是其他my GUEST模板的来宾,并且用户是客户键入我的客户模板 我有以下模板
<!-- 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绑定到ViewModel中的CurrentUser属性。然后根据您放置的类型选择正确的模板。 您的实体需要从相同的基类派生:
public abstract class BaseEntity
{
}
public class Customer : BaseEntity
{
}
public class Guest:BaseEntity
{
}
您的视图模型:
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>
我不清楚您在问什么-是否要更改列表框项目中的模板?还是在用户详细信息网格中?是否基于谁在使用应用程序或列表框中选择了什么项目?在用户详细信息网格中,基于谁在使用应用程序。请注意,如果出现安全问题,错误类型的用户看到错误类型的详细信息,则应在代码隐藏中处理。否则,使用Snoop WPF之类的工具很容易更改绑定等。