使用mvvm架构,在某一个自定义控件内部或者DataGrid内部调用外部view的DataContext。
<TextBlock Text="{Binding DataContext.WatermarkText,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type settingCtrl:SettingView}}}"/>
binding里面设置RelativeSource的值即可 注意Binding里面要在数据源之前加 DataContext
<xceedToolkit:WatermarkTextBox Name="TxtSearchBar" Width="262" FontSize="16" FontFamily="Source Sans Pro" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" Background="#ffffff" BorderBrush="#7c868f" BorderThickness="1,1,1,1" Text="{Binding SearchValue, UpdateSourceTrigger=PropertyChanged}" Margin="10,0,0,0"> <xceedToolkit:WatermarkTextBox.WatermarkTemplate> <DataTemplate> <TextBlock Name="txtWatermark" Text="{Binding DataContext.WatermarkText,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type settingCtrl:SettingView}}}" FontSize="14" Margin="5,0,0,0" Foreground="Gray" /> </DataTemplate> </xceedToolkit:WatermarkTextBox.WatermarkTemplate> </xceedToolkit:WatermarkTextBox>