나는 (아래 XAML을) 그리드에 데이터 컨텍스트를 결합하려고했다WPF 바인딩 루트 요소가 작동하지 않습니다
<Grid .... DataContext="{Binding Path=NewFormViewModel}" > ...</Grid>
이 작동하지 않았다 바인딩 및 NewFormViewModel는 결코 존재하지 않았다 위해 나는 게터를 실현 라는.
이 시점에서 그리드는 xaml 파일의 루트 요소였습니다.
나는 다음 그리드 내부에 캔버스를 배치하고 같은 캔버스에 바인딩 않았다
<Grid ....>
<Canvas DataContext="{Binding Path=NewFormViewModel}">
....
</Canvas>
</Grid>
데이터는 일 바인딩.
<Canvas.... DataContext="{Binding Path=NewFormViewModel}" > ...</Canvas>
다시 작동이 중지 바인딩 :
다음 나는 캔버스에 그리드를 변경하고 다음과 같이 agiain 데이터 바인딩 할 노력했다.
이<Canvas....>
<Grid DataContext="{Binding Path=NewFormViewModel}">
....
</Grid>
</Canvas>
문제는 이유를하여 XAML의 루트 요소에 바인딩이 작동하지 않았다입니다 : 나는 캔버스 안에 중첩 그리드에 대한 정착 결국
?또는 내가 루트 요소로 Canvas/Grid를 사용하지 않아야하고 Page/UserControl과 같은 것을 사용해야합니까?
편집
내 논리적 나무는 다음과 같이 뭔가를해야만 같습니다
Window <- Data binding to object o
|
*
Frame <-Data binding to obect o inherited
|
*
Canvas/Grid <- Data binding to o.NewFormViewModel failed
|
*
Canvas/Grid <- Data binding to o.NewFormViewModel Succeeds
EDIT2 :
깨진 XAML : 위의 XAML에서
<Canvas x:Class="WPFEditors.NewForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="398" Width="377"
DataContext="{Binding Path=NewFormViewModel}"
>
<Grid >
<Label DataContext="Form Type" Height="31" HorizontalAlignment="Left" Margin="12,22,0,0" VerticalAlignment="Top" Width="92" />
<ComboBox Margin="148,22,6,347" ItemsSource="{Binding Path=FormTypes}" />
<Label Content="Description" Height="31" HorizontalAlignment="Left" Margin="12,58,0,0" VerticalAlignment="Top" Width="92" />
<Label Content="{Binding Path=Heading}" ToolTip="This is pulled from the Enum defined for FormTypes" Margin="148,59,6,309" />
<Label Content="Version" Height="31" HorizontalAlignment="Left" Margin="12,95,0,0" VerticalAlignment="Top" Width="92" />
<TextBox Text="Bind this later" Margin="148,97,6,270" Height="31" />
<Label Content="Approval Level" Height="31" HorizontalAlignment="Left" Margin="12,132,0,0" VerticalAlignment="Top" Width="92" />
<TextBox Height="31" Margin="148,134,6,233" Text="Bind this later" />
<Label Content="Number of Approvals" Height="31" HorizontalAlignment="Left" Margin="12,171,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,173,6,194" Text="Bind this later" />
<Label Content="Heading" Height="31" HorizontalAlignment="Left" Margin="12,210,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,212,6,155" Text="Bind this later" />
<Label Content="Static Data Id" Height="31" HorizontalAlignment="Left" Margin="12,247,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,249,6,118" Text="Bind this later" />
<Label Content="{Binding Path=Errors}" Background="{Binding Path=Color}" Margin="12,325,6,6" BorderThickness="1" BorderBrush="#FF000019" />
<Button Content="Create" Margin="83,294,202,78" />
<Button Content="Create" Margin="181,294,104,78" />
<Button Content="Create" Margin="279,294,6,78" />
</Grid>
</Canvas>
다음 줄의 바인드를 :
표제가이 문서에서 상속하는보기 모델의 속성 내에 있더라도. 는이 시점에서 나는 선을 예상했을 것 :
DataContext="{Binding Path=NewFormViewModel}"
에는 제목이 없습니다 NewFormViewModel에 데이터 컨텍스트를 변경 할 수 있습니다. 나머지 바인딩은 모두 실패합니다.
I가 시작을 변경하는 경우 :
<Canvas x:Class="NewForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="398" Width="377"
>
<Grid DataContext="{Binding Path=NewFormViewModel}" >
제목에 바인딩 실패하고 바인딩의 나머지 작업을 시작합니다. 이것은 내가 처음에 기대했던 행동입니다.
이 XAML이 파일에 중첩되어
<Window x:Class="WPFEditors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=Heading}" Height="743" Width="1177">
<StackPanel >
<Menu IsMainMenu="True" >
<MenuItem Header="New">
<MenuItem Header="New Form" Command="{Binding Path=MenuCommand}" />
</MenuItem>
<MenuItem Header="Edit Form" ItemsSource="{Binding Path=FormsAvailable}" />
<MenuItem Header="Edit Rules" />
</Menu>
<Frame NavigationUIVisibility="Hidden" Source="{Binding Path=CurrentPage}"
LoadCompleted="Frame_LoadCompleted"
DataContextChanged="Frame_DataContextChanged"
Name="frame">
</Frame>
</StackPanel>
</Window>
이벤트 핸들러가 아이에게 데이터 컨텍스트를 복사 같습니다 루트 요소에 바인딩
private void Frame_LoadCompleted(object sender, NavigationEventArgs e)
{
UpdateFrameDataContext();
}
private void Frame_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
UpdateFrameDataContext();
}
private void UpdateFrameDataContext()
{
var content = frame.Content as FrameworkElement;
if (content == null)
return;
content.DataContext = frame.DataContext;
}
정확히 내가 처음 생각한 것입니다. 그러나 위의 네 가지 예제 모두에서 컨트롤 계층 구조는 항상 같았습니다. 즉 xaml 파일을로드하는 프레임은 항상 동일하게 유지되었으며 프레임의 dataContext는 항상 동일했습니다. 그러나 xaml의 루트 요소는 어떤 이유에서든 어린이가 바인딩하는 동안 바인딩하지 않습니다. 나는 이것이 왜 일어나고 있는지 궁금 할뿐입니다. 귀하의 의견을 보내 주셔서 감사합니다. –
그리고 children 및 dataContext의 바인딩 작업은 자식에 의해 상속되므로, 부모도 동일한 dataContext를 가질 것이라고 가정하는 것이 안전하다고 생각합니다. 특히 주 윈도우에서 설정 한 DataContext를 사용하고 있기 때문에 특히 그렇습니다. –
어떻게 채워지고 작동하지 않는지에 대한 전체 예제를 볼 수 있습니까? – NestorArturo