개체의 형식을 가져 와서 개체로 동적으로 설정할 수 있습니까? 나는 모두가 같은 속성을 포함하고 내가 THEDYNAMICTYPE개체의 형식을 가져 와서 개체로 동적으로 설정하십시오.
어떤 제안으로 this.DataContext을 말하고 싶은 CamerasViewModel로 this.DataContext 말하는 대신이
if (this.DataContext is CamerasViewModel)
{
//Type type = Type.GetType((this.DataContext.ToString());
object o = Assembly.GetExecutingAssembly().CreateInstance(this.DataContext.GetType().ToString());
Type type = o.GetType();
foreach (ButtonViewModel button in (this.DataContext as type).Buttons)
{
if (button.DisplayName == this.Content.ToString())
{
this.Template = (ControlTemplate)this.FindResource(button.TemplateResource.Substring(0, button.TemplateResource.Length - 3) + "pr");
break;
}
}
}
그런 짓을 할 몇 가지 ViewModels 있나요?
아니요, 할 수 없습니다. 그리고 그렇게했다면 다음 단계는 무엇입니까? 또한 이미 정적 검사를 수행합니다. 나는 왜 당신이 그것에 문제가 있는지 알지 못한다. – leppie
여기서 무엇을하려고합니까? 당신은 이미 this.DataContext가 첫 번째 "if"문으로 인해 CamerasViewModel이라는 것을 알고 있습니다. "Buttons"라는 ButtonViewModels 컬렉션이있는 모든 객체에서 button.DisplayName을 설정하려고합니까? –
다른 모든 ViewModel 유형의 경우 문제는 tedius입니다. 그러나 귀하의 의견을 바탕으로, 불가능한 것 같습니다. Activator.CreateInstance로는 아무 것도 충분하지 않을까요? – bl4kh4k