简体   繁体   English

xaml Windows Phone 8.1从模板绑定到页面

[英]xaml windows phone 8.1 binding to page from template

I am trying to bind the visibility on a checkbox that is in a DataTemplate (used in a listbox) to a property on my ViewModel. 我正在尝试将DataTemplate(用于列表框)中的复选框上的可见性绑定到ViewModel上的属性。 I want to show and hide all the checkbox's with the one property. 我想显示和隐藏具有一个属性的所有复选框。

After all my searching I can't find the solution. 经过所有搜索,我找不到解决方案。 Here is the non-working code that I have so far. 这是到目前为止我无法使用的代码。

<DataTemplate x:Key="GroupTemplate">
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <CheckBox  Visibility="{Binding CheckboxVisible, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}"  Grid.Column="0" />
        <TextBlock  Text="{Binding GroupName}" Grid.Column="1"/>
    </Grid>
</DataTemplate>

public class GroupListViewModel : ViewModelBase
{
    private bool _checkboxVisible;
    public bool CheckboxVisible
    {
        get
        {
            return _checkboxVisible;
        }
        set
        {
            Set(() => CheckboxVisible, ref _checkboxVisible, value);
        }
    }               
    // ...
}   

is GroupListViewModel the DataContext of the items control that has this data template? GroupListViewModel是具有此数据模板的项目控件的DataContext吗? Then you can add a name to the items control and access it by name, for example: 然后,您可以将名称添加到项目控件并按名称访问它,例如:

<GridView x:Name="items">
...
<CheckBox  Visibility="{Binding DataContext.CheckboxVisible, ElementName=items, Converter={StaticResource BooleanToVisibilityConverter}}"  Grid.Column="0" /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM