无法绑定模板化组合框中的选定项目

时间:2013-09-11 10:36:43

标签: c# wpf combobox

我有这个修改过的ComboBox:

<ContentControl Content="{Binding LoadedFiles}" >
   <ContentControl.ContentTemplate>
      <DataTemplate>
         <Grid>
            <ComboBox x:Name="cb" ItemsSource="{Binding}" Width="150" Margin="5"   SelectedItem="{Binding SelectedFile}" DisplayMemberPath="FileName"/>
            <TextBlock x:Name="tb" Text="Select a file" IsHitTestVisible="False" Visibility="Hidden" Width="150" VerticalAlignment="Center" Margin="10 0 0 0" FontSize="12" FontStyle="Italic" Foreground="#7FFFFFFF" />
         </Grid>
         <DataTemplate.Triggers>
            <Trigger SourceName="cb" Property="SelectedItem" Value="{x:Null}">
               <Setter TargetName="tb" Property="Visibility" Value="Visible"/>
            </Trigger>
         </DataTemplate.Triggers>
      </DataTemplate>
   </ContentControl.ContentTemplate>
</ContentControl>

它的作用是在所选项目为空时显示默认文本。 ItemsSource效果很好。整个组合框在使用ContentControl之前运行良好。现在我无法正确绑定SelectedItem,即使我更改了组合框的选择,实际对象仍然保留为初始化时。

我读过这解决了这个问题:

SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedFile}"

但这对我没有帮助,这里发生了什么?为什么我不能像以前那样绑定?

1 个答案:

答案 0 :(得分:0)

错误...你可能希望看看MSDN上的BindingBase.TargetNullValue Property页面,以解决在数据绑定属性为{时指定要显示的值的原始问题{1}}。你可以这样使用它:

null

重新发明轮子毫无意义。

相关问题