Let a block diagonal matrix reads $$B := {\rm blkdiag}\left(A_1, \cdots, A_i, \cdots, A_N \right) \ \in \mathbb{R}^{MN \times KN} \ ,$$ where $A_i \in \mathbb{R}^{M \times K}$.
How to take the derivative of $f = {\rm tr} \left[ U^T \; {\rm unvec} \left( B \ {\rm vec}(X) \right) \right]$, where $U \in \mathbb{R}^{M \times N}$ and $X \in \mathbb{R}^{K \times N}$ w.r.t. $X$?
Define the vectors $$\eqalign{ x &= {\rm vec}(X) \cr u &= {\rm vec}(U) \cr }$$ Write your function in terms of the vectors. Then find the differential and gradient. $$\eqalign{ f &= u^TBx = (B^Tu)^Tx \cr df &= (B^Tu)^T\,dx \cr \frac{\partial f}{\partial x} &= B^Tu \cr }$$ Now de-vectorize this to obtain a matrix result. $$\eqalign{ \frac{\partial f}{\partial X} &= {\rm unvec}(B^Tu) \cr }$$