在wpf中分配组合框值

时间:2016-10-10 06:27:49

标签: wpf combobox

请告诉我如何使用以下代码将分配给组合框,并指定显示文字

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

 using namespace cv;
 using namespace std;

 Mat src; Mat src_gray;
 int thresh = 5;
 int max_thresh = 60000;
 RNG rng(12345);

 void thresh_callback(int, void* );

 static double angle(Point pt1, Point pt2, Point pt0)
 {
     double dx1 = pt1.x - pt0.x;
     double dy1 = pt1.y - pt0.y;
     double dx2 = pt2.x - pt0.x;
     double dy2 = pt2.y - pt0.y;
     return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
 }

 void setLabel(cv::Mat& im, const std::string label, std::vector<cv::Point>& contour)
 {
    int fontface = cv::FONT_HERSHEY_SIMPLEX;
    double scale = 0.4;
    int thickness = 1;
    int baseline = 0;

    cv::Size text = cv::getTextSize(label, fontface, scale, thickness, &baseline);
    cv::Rect r = cv::boundingRect(contour);

    cv::Point pt(r.x + ((r.width - text.width) / 2), r.y + ((r.height + text.height) / 2));
    cv::rectangle(im, pt + cv::Point(0, baseline), pt + cv::Point(text.width, -text.height), CV_RGB(255,255,255), CV_FILLED);
    cv::putText(im, label, pt, fontface, scale, CV_RGB(0,0,0), thickness, 8);
 }


int main()
{

    cv::Mat src = cv::imread("p3.png");
    resize(src, src, Size(640,480), 0, 0, INTER_CUBIC);

     char* source_window = "Source";
     namedWindow( source_window, CV_WINDOW_AUTOSIZE );
     imshow( source_window, src );

     createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
     thresh_callback( 0, 0 );

waitKey(0);
return(0);
}

void thresh_callback(int, void* ) {

    src = imread("p3.png");
    resize(src, src, Size(640,480), 0, 0, INTER_CUBIC);
    //================================
    Mat gray,bw,dil,erd;

    Canny(gray,bw,thresh, thresh*1, 5);
    //Canny( src_gray, canny_output, thresh, thresh*1, 3 );
    dilate(bw,dil,Mat());
    erode(dil,erd,Mat());
    Mat tmp=bw.clone();

    Size kernalSize (15,20);
    Mat element = getStructuringElement (MORPH_RECT, kernalSize, Point(4,4)  );
    morphologyEx( bw, bw, MORPH_CLOSE, element );
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;//novo
    findContours(bw.clone(), contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    vector<Point> approx;
    Mat dst = src.clone();

    for( int i = 0; i< contours.size(); i++ )
    {
        approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.09, true);

        if (approx.size() >= 4.0 && (approx.size() <= 4.1))
        {
            int vtc = approx.size();
            vector<double> cos;
            for(int j = 2; j < vtc + 1; j++)

            cos.push_back(angle(approx[j%vtc], approx[j-2], approx[j-1]));
            sort(cos.begin(), cos.end());
            double mincos = cos.front();
            double maxcos = cos.back();

            if (vtc == 4 && mincos >= -0.6 && maxcos <= 0.6)
            {
                Rect r = boundingRect(contours[i]);
                double ratio = abs(1 - (double)r.width / r.height);
                if (r.height >50 && r.width >20 && r.height < 640 && r.width < 640 && r.height>r.width ) /* constraints on region size */
                                {
                //Rect r = boundingRect(contours[i]);
                //double ratio = abs(1 - (double)r.width / r.height);
                line(dst, approx.at(0), approx.at(1), cvScalar(0,0,255),4);
                line(dst, approx.at(1), approx.at(2), cvScalar(0,0,255),4);
                line(dst, approx.at(2), approx.at(3), cvScalar(0,0,255),4);
                line(dst, approx.at(3), approx.at(0), cvScalar(0,0,255),4);
            }
          }
        }
    }

    }

4 个答案:

答案 0 :(得分:0)

您必须在viewModel中创建一个名为Name的字符串列表,以实现INotifyPropertyChanged

答案 1 :(得分:0)

您只需使用DisplayMemberPath属性设置将显示的DataTable列的名称。

代码背后:

ComboBox.ItemsSource = dt.AsDataView();

XAML:

<ComboBox x:Name="ComboBox" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="256,41,0,-6"
    VerticalAlignment="Top" Width="108" Height="26" FontSize="13">
</ComboBox>

答案 2 :(得分:0)

<强> XAML:

<ComboBox x:Name="ComboBox" ItemsSource="{Binding myListofItems}" HorizontalAlignment="Left" Margin="256,41,0,-6" VerticalAlignment="Top" Width="108" Height="26" FontSize="13" />

<强> C#:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private List<string> _myListOfItems = new List<string> ();
    public List<string> myListOfItems
    {
        get { return (_myListOfItems); }
        set
        {
            _myListOfItems = value;
            OnPropertyChanged ("myListOfItems");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) {
            handler (this, new PropertyChangedEventArgs (propertyName));
        }
    }

    public MainWindow()
    {
        InitializeComponent ();
        this.DataContext = this;

        // Start Populating your List here
        // Example:
        for (int i = 0; i < 10; i++) {
            myListOfItems.Add (i.ToString ());
        }
    }
}

答案 3 :(得分:0)

apt-get