English 中文(简体)
WPF 数据网与ToggleButton有约束力
原标题:WPF datagrid with ToggleButton binding

我对世界森林论坛来说相对较新,现在很难用时间约束一个定制类别进入数据网。 虽然文字特性是K(只有读取的一条路),但我的项目清单中并未更新用于养蜂的 to,也没有按照最初确定的价值加以展示。 然而,他们的确对“国际钻石调查”中的点击作出了正确的反应。

<Style x:Key="ToggleImageStyleBien" TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel>
                    <Image Name="img" Source="Images/transp.png"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter TargetName="img" Property="Source" Value="Images/good.png"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

《数据集》本身:

<DataGrid AutoGenerateColumns="False" Height="Auto" Name="dataGridRevision" Width="Auto" Margin="6,6,6,0" ItemsSource="{Binding}" VerticalScrollBarVisibility="Auto" VerticalGridLinesBrush="{x:Null}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Code" Binding="{Binding Code}" Visibility="Collapsed"/>
        <DataGridTextColumn Header="Descripción" Binding="{Binding Description}" IsReadOnly="True"/>
        <DataGridTemplateColumn Header="Bien">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ToggleButton Style="{StaticResource ToggleImageStyleBien}" Click="ToggleButton_Click" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=ReviewItem.Good, Mode=TwoWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Comentario" />
    </DataGrid.Columns>
</DataGrid>

这是一米级,对:

public class ReviewItem
{
    public string Code { set; get; }
    public string Description { set; get; }
    public bool Good { set; get; }
    public bool Bad { set; get; }
    public string Comment { set; get; }
}

就我所知,我不使用ToggleButton的受赠财产,但我曾尝试过很多东西,没有想法。 清单中的财产没有在点上变化,这些数值没有按照数据显示。

Please help...

感谢!

Joerg。

根据其他例子,将这一类改为:

public class ReviewItem : INotifyPropertyChanged
{
    public string Code { set; get; }
    public string Description { set; get; }
    public bool Bad { set; get; }
    public string Comment { set; get; }

    private bool _isChecked;
    public bool Good
    {
        get { return _isChecked; }
        set
        {
            System.Diagnostics.Debug.WriteLine("Good = " + value);
            _isChecked = value;
            OnPropertyChanged("Good");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
最佳回答

您目前对<代码>UserControl.ReviewItem具有约束力,该编码在<编码>UserControl上不属于有效财产。

Try binding to DataContext.ReviewItem und which will binding to UserControl.DataContext.ReviewItem

如果您无意对<代码>UserControl.DataContext.ReviewItem.Good具有约束力,而是希望对具有约束力。 DataGrid.ReviewItems [x]. 良好,因此,你只需要约束<代码>IsChecked=”{约束良好}>。 这是因为,在<代码>DataGridCell/code>上的缺省代码是各行各式各样的数据项目,因此,如果您的收集包含一份ReviewItems的清单,则该单元的数据内容已定在上。 ReviewItem

Also, I would highly recommend using Snoop for debugging WPF bindings. You can use it on your application to see what the DataContext is for individual UI Elements, and figure out if your bindings are correct or not.

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...