WPF,有条件地将项绑定到详细视图

时间:2015-01-29 02:12:40

标签: 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"
                         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和客户  他们有不同的属性

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

我有以下的Templetes

         <!--  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>

1 个答案:

答案 0 :(得分:2)

您应该将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>