2014-05-02 81 views
2

我有一個用戶控件的文本框和按鈕,它們綁定到來自viewmodel的屬性。當沒有綁定元素時,使默認可見性崩潰

<Grid> 
     <StackPanel> 
      <TextBox Text=" Hi" IsEnabled="{Binding IsReadOnly, UpdateSourceTrigger=PropertyChanged}"></TextBox> 
      <Button Content="B" Visibility="{Binding IsVisible, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BooleanToVisibilityConverter}}"></Button> 
     </StackPanel> 
    </Grid> 

如何使該按鈕在不爲此usercontrol提供datacontext時不可見?

回答

6

FallbackValue

<Button Content="B" Visibility="{Binding IsVisible, 
           UpdateSourceTrigger=PropertyChanged, 
           Converter={StaticResource BooleanToVisibilityConverter}}, 
           FallbackValue=Collapsed"></Button> 
+0

非常感謝你 – user3013076