WPF:有没有办法创建动态模板

时间:2017-04-03 07:00:33

标签: c# wpf

不幸的是我必须在学校的WPF中创建一个应用程序。我不太了解它,但我很了解Javascript和一些JS框架。 使用Ember / Handelbars,创建模板非常简单。使用动态模板我的意思是:我有一个标记(xaml),我可以放入数据/模型,模板根据数据呈现。然后我可以多次使用此模板,每次都放入不同的模型。

如何在WPF中创建动态模板? 目前我有

<?php

class ControllerSaleno extends Controller {
    private $error = array();
    public function index() {

           $data=$this->request->post;
           $customerId=$data['customer_id'];

        //$this->sendMail($customerId);

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        //$this->response->setOutput($this->load->view('default/template/product/no.tpl', $data));

        $this->response->setOutput($this->load->view('sale/no.tpl', $data));


        //$data['productsData']=$this->getProductData();
         $customers=$this->getCustomerData();

             $data['customerData']=array();


 foreach($customers as $customer)
{
   $data['customerData'][]=array(
'customer_id'=>$customer['customer_id'],
'customer_name'=>$customer['firstname'].' '.$customer['lastname']
);
   //var_dump($data['customerData']);
   //var_dump($customer['firstname']);
}


         $data['NoOrder']=$this->url->link('sale/order/NoOrder', 'token=' . $this->session->data['token'] , 'SSL'); //added code
         $this->response->setOutput($this->load->view('sale/no.tpl', $data)); //added code

    }
    public function getCustomerData()
    {

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer order by firstname asc");
        return $query->rows;
    }

    /*no order*/
    public function NoOrder()
    {


        $this->sendMail();
        //$this->response->redirect($_SERVER['HTTP_REFERER']);
    }

    public function sendMail()
    {

        $tempArray=array();

      $selectedCustomerId=$customerId;
            $query=$this->db->query("SELECT state FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
            $state=$query->row;





            $querySalesEmail=$this->db->query("SELECT email FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
            $salesPersonEmail1=$querySalesEmail->row;

            $salesPersonEmail= $salesPersonEmail1['email'];


 $queryCustomerName=$this->db->query("SELECT concat(firstname,' ',lastname) as name FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
$customerName=$queryCustomerName->row['name'];






$To = "radhamani123@gmail.com";
   $Subject = "New Order From " . "'" . $customerName . "'" . date('m/d/Y h:i:s');
    $Headers  = "MIME-Version: 1.0\r\n";
    $Headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $Headers .= "From: No ORder<orders@milkymist.in>\r\n";

   $Message = '';


            $Message .= 'No Order Today';


    if(mail($To, $Subject, $Message, $Headers)){
        echo "success";



    }else{
        echo "failure";
    }



        //$this->response->setOutput(json_encode($result));
 $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

       $this->response->setOutput($this->load->view('sale/noorder.tpl', $data));
        //$this->response->setOutput($this->load->view('sale/no.tpl', $data));

    }
    /*no order*/

}

如何让它变得动态,以便敌人可以变量数据? 然后我可以多次使用它,如:

<Page.Resources>
  <Grid x:Key="char" Margin="-5,5,5,0">
    <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{Binding Path=variableModel.Name}" FontSize="16" Height="18"/>
    <ProgressBar Maximum="{Binding Path=variableModel.MaxHealth, Mode=OneWay}" Value="{Binding Path=variableModel.Health, Mode=OneWay}" Height="8"/>
  </Grid>
</Page.Resources>

所以现在我会展示两个角色,一个是敌人模特,另一个是玩家。两者都使用相同的模板。

我知道有一些样式或数据模板,但我不知道如何为多个元素制作一个模板......

1 个答案:

答案 0 :(得分:1)

定义DataTemplate

<Page.Resources>
    <DataTemplate x:Key="char">
        <StackPanel Margin="-5,5,5,0">
            <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{Binding Path=Name}" FontSize="16" Height="18"/>
            <ProgressBar Maximum="{Binding Path=MaxHealth, Mode=OneWay}" Value="{Binding Path=Health, Mode=OneWay}" Height="8"/>
        </StackPanel>
    </DataTemplate>
</Page.Resources>

然后,您可以使用ContentControl并将Content属性设置为具有NameHealthMaxHealth属性的类的实例:

<ContentControl x:Name="cc" ContentTemplate="{StaticResource char}" />
cc.Content = new Player();