Bijective function on a dense set Suppose X is a complete metric space, and $f:X↦X$ a continuous surjective function. Let D be a dense set. Suppose $f:D↦D$ is injective and $f^{-1}(D)=D$.   
Is $f$ injective ?
Is there a family of metric spaces where you can conclude  $f$ is injective?
 A: Here is another example which is easy to visualize. Let $X=[-1,1]^2$, $I$ be the segment between $(-1,0)$ and $(1,0)$ and $I_0$ its subsegment between $(-1/2,0)$ and $(1/2,0)$. There is a homeomorphism $f$ between $X\setminus I_0$ and $X\setminus \{0\}$ which sends $I\setminus I_0$ to $I\setminus\{0\}$ and extends continuously by setting $f(I_0)=0$. (Think of contracting the segment $I_0$ to the center while deforming the rest of the square continuously.) This map can be given by an explicit formula which is piecewise linear on each horizontal segment. Now define $D=X\setminus I$ and we are done. 
A: The answer to the second question is "yes, for discrete spaces where metric is $d(x, y) = 1,$ for $x \neq y.$
A: Here's an example. First take a countable family $\lbrace A_n:n\in\mathbb{N}\rbrace$ of countable dense sets in $(0,1)$ and then let $f:[0,1]\to\[0,1]$ be continuous such that for each $n$ the maps $f$ is an order-isomorpism between $A_n\cap(0,1/1)$ onto $A_{2n}$ and an order-reversing isomorphism between $A_n\cap(1/2,1)$ and $A_{2n+1}$. Thus $f$ is a bijection between $D=\bigcup_{n]1}^\infty A_n$ and itself, but it is not injective on $[0,1]$. The map $f$ is readily constructed, first recursively on $D$ and then by continuous extension to all of $[0,1]$. 
Addendum: to construct the restriction of $f$ to $[0,1/2]$ modify Cantor's proof of the uniqueness of $\mathbb{Q}$ to construct an order-isomorphism $g$ between $D\cap(0,1/2)$ and $E=\bigcup_{n=1}^\infty A_{2n}$ that maps $A_n\cap(0,1/2)$ onto $A_{2n}$ for each $n$. Then define $f(x)=\sup\lbrace g(d):d\in D, d < x \rbrace $. Define $f$ on $[1/2,1]$ in a similar fashion.
Another addendum: I missed/overlooked the condition that $D=f^{-1}[D]$; my map is a bijection from $D$ to itself and that's it. The near-homeomorphisms on the square settle it nicely. In the spirit of those constructions one can get a one-dimensional example on the $\sin\frac1x$-curve: in every arc between points with coordinates $((n+\frac12)\pi)^{-1}$ and $((n+\frac32)\pi)^{-1}$ shrink the interval of points with $y$-coordinates between $-1/2$ and $1/2$ to the interval $[-1/n,1/n]$, everything in a bijective way. Then on the limit segment $\lbrace0\rbrace\times[-1,1]$ the interval $[-1/2,1/2]$ is collapsed to a point. Here $D$ is the graph of $\sin\frac1x$ of course. 
A: I think I can prove that if X=[0,1] (or 1-dimensional) then the claim of FelipeG is true.
Indeed, let $X=[0,1]$ and $D,f$  be as above, and proceed by contradiction. Assume there are points $a,b\in [0,1]$ such that $a<b$ and $f(a)=f(b)$; then, since  $(a,b)\cap D$ has infinitely many elements, then there is some point $c\in D \cap (a,b)$ such that $f(c)=s \neq r$ (say $s>r$).
On the other hand, by continuity of $f$, for every value $y\in D\cap (r,s)$ (which is not empty) there is an element $x_1\in (a,c)$ such that $ f(x_1) = y $ and an element $x_2\in (c,b)$ such that $ f(x_2)= y $. But, since $f^{-1}D=D$ both $x_1, x_2$ belong to $D$, which contradits the injectivity of $f_{|_D}$.
PS: this construction works if $f_{|_D}$ is a bijection (as one might argue from the title), otherwise there  is a counterexample
A: There are counterexamples. 
Let $X$ be the ordinal $\omega^2+1$, which as a topological
space is the same as infinitely many convergent sequences,
whose limit points converge. This space is homeomorphic to
a countable closed subset of the unit interval and is
therefore completely metrizable.  Let $D$ be the isolated points of $X$, which is exactly the
set of successor ordinals below $\omega^2$. This is dense,
since the closure adds the missing limit ordinals. Let $f$
be the function that interleaves two successive sequences
together into one. That, we combine the successor ordinals
in the interval $[\omega\cdot 2n,\omega\cdot 2(n+1))$ to those in
$[\omega\cdot n,\omega\cdot(n+1))$ by an injective function
that simply interleaves the two sequences into one. This is
injective on $D$ and also surjective. The function $f$
extends continuously to $X$ by mapping the limit points of
the successive sequences, $\omega\cdot2n$ and
$\omega\cdot2(n+1)$ both to $\omega\cdot n$, and
$\omega^2\mapsto \omega^2$. Note that the extension $f$ is
not injective.
A simpler version of this example, without ordinals, is to take $X$ to be the positive integers, plus a sequence converging to each of them in the interval below.  Specifically, let $X$ have points $k$ and also $k-\frac 1n$ for positive integers $k$ and $n$. Thus, $X$ is a countable closed subset of $\mathbb{R}$. Let $D$ be the isolated points $k−\frac1n$, and let $f$ be the function that interleaves successive convergent sequences into one. That is, for each adjecent pair of sequences, converging to $2n$ and $2(n+1)$, we map the isolated points of the sequences bijectively to the sequence converging to $n$.  This is bijective on D, but extends continuously to $X$, and is not injective on $X$. 
(I have removed the earlier flawed example.)
A: Sorry but I could not resist:
    
The map is defined on $[0,1]\times[0,1]$, and can be written as
$$
 f(x,y) = \begin{cases} 
             (x, (2-x)y) & \text{ if }y\leq 1/2, \\
             (x, xy+1-x) & \text{ if }y> 1/2.
         \end{cases}
$$
Just take $D$ to be the set of $(x,y)$ with rational coordinates in $(0,1)\times(0,1)$, then $f$ is  bijective on $D$ (because it can be easily inverted), but it is clearly not injective on $[0,1]\times[0,1]$.
