开发者

How to get datacontext from stackpanel

开发者 https://www.devze.com 2023-01-05 13:18 出处:网络
I have: <StackPanelDataContext=\"{Binding Path =MyContext}\"> <TextBox Text=\"{Binding Path =Content}\" x:Name=\"tbName\" IsReadOnly=\"False\">

I have:

<StackPanel  DataContext="{Binding Path =MyContext}">
    <TextBox Text="{Binding Path =Content}" x:Name="tbName" IsReadOnly="False">
    </TextBox>
    <CheckBox x:Name="cboxName" Content="Is null ?" Click="cboxName_Click" IsChecked="{Binding Path=THIS, Converter={StaticResource MyContextToBoolConverter}}">
     </CheckBox>
</StackPanel>

public class MyContextToBoolConverter: IValueConverter
{
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         return (value!=null);
     }

     public object ConvertBack(object v开发者_运维百科alue, Type targetType, object parameter, CultureInfo culture)
     {
            return parameter;
     }
}

I just only want to get DataContext to checkbox from StackPanel.


You should replace THIS with . or completely remove the Path from the Binding. This will create a binding directly to the DataContext.

IsChecked="{Binding Converter={StaticResource MyContextToBoolConverter}}"


Or try this -

<StackPanel x:Name="StackPanel" DataContext="{Binding Path =MyContext}"> 
   <TextBox Text="{Binding Path =Content}" x:Name="tbName" IsReadOnly="False" /> 
   <CheckBox x:Name="cboxName" Content="Is null ?" 
       Click="cboxName_Click" 
       IsChecked="{Binding ElementName=StackPanel, Path=DataContext, Converter={StaticResource MyContextToBoolConverter}}"> 
   </CheckBox> 
</StackPanel> 
0

精彩评论

暂无评论...
验证码 换一张
取 消