【问题标题】:Set Property of UserControl in DataGrid?在 DataGrid 中设置 UserControl 的属性?
【发布时间】:2021-02-11 09:57:18
【问题描述】:

我正在开发一个播放音频文件的程序,但我的一个 DataGrids 遇到了一些问题。基本上它的目的是允许用户更改它的设置(例如听、重复和音量)

为此,我制作了自己的 UserControl,它是通过 XAML 中的 DataTemplates 动态添加的。所以首先这里有一些背景;

SoundFile.cs (公共类)
具有文件相关属性(例如名称、路径、类别和卷)的类。

SoundLibrary.cs (公共静态)
这是一个静态类,用于跟踪 ObservableCollection 中的所有 SoundFiles,这是用于填充我的 DataGrids 的集合。

SoundControl.cs (UserControl)
这是我的 UserControl,它由几个按钮组成,它有一个 SoundFile 属性,我对如何设置和/或正确访问感到困惑。

这就是我的 DataGrid 的定义方式;

<DataGrid
    x:Name="MyDataGrid"
    Margin="0,0,0,36"
    AlternationCount="2"
    AutoGenerateColumns="False"
    IsReadOnly="True"
    ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        <DataGridTemplateColumn Header="Listen Volume">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Slider
                        Margin="5"
                        Maximum="100"
                        Minimum="0"
                        Value="{Binding DataContext.ListenVolume, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Settings">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <local:SoundControl x:Name="SoundController" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Progress">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ProgressBar
                        Maximum="100"
                        Minimum="0"
                        Visibility="Visible"
                        Value="50" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这是我在后面的代码中绑定它的方法;

MyDataGrid.ItemsSource = SoundLibrary.Library;

for (int i = 0; i < MyDataGrid.Items.Count; i++)
{
    SoundFile sf = (SoundFile)MyDataGrid.Items.GetItemAt(i);
    Debug.WriteLine(sf.FilePath);
}

这样做我可以按预期访问 SoundFile,但是我不确定如何通过 SoundControl 访问 SoundFile。

SoundControl 类很简单;

public partial class SoundControl : UserControl
{
    public SoundFile Sound { get; set; } = new SoundFile();

    public SoundControl()
    {
        InitializeComponent();

        ListenButton.IsToggled = Sound.Listen;
        RepeatButton.IsToggled = Sound.Repeat;
    }

    private void GridButton_MouseUp(object sender, MouseButtonEventArgs e)
    {
        if (sender == ListenButton)
        {
            Sound.Listen = ListenButton.IsToggled;
            MainWindow mainWindow = (MainWindow)Window.GetWindow(this);
            mainWindow.UnsavedChanges = true;
        }

        if (sender == RepeatButton)
        {
            Sound.Repeat = RepeatButton.IsToggled;
            MainWindow mainWindow = (MainWindow)Window.GetWindow(this);
            mainWindow.UnsavedChanges = true;
        }

        if (sender == FolderButton)
        {
            Debug.WriteLine(Sound.FilePath);
            //Sound.OpenDirectory();
        }
    }
}

任何指向正确方向的指针都将不胜感激,对于数据绑定和使用 DataGrids 来说仍然相对较新...在此先感谢!

【问题讨论】:

    标签: c# wpf class binding datagrid


    【解决方案1】:

    SoundControl 的Sound 属性应该是可绑定属性,即依赖属性:

    public static readonly DependencyProperty SoundProperty =
        DependencyProperty.Register(
            nameof(Sound), typeof(SoundFile), typeof(SoundControl));
    
    public SoundFile Sound
    {
        get { return (SoundFile)GetValue(SoundProperty); }
        set { SetValue(SoundProperty, value); }
    }
    

    您可以像这样将它绑定到 DataGrid 行的当前 SoundFile 项:

    <DataGridTemplateColumn Header="Settings">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <local:SoundControl Sound="{Binding}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    【讨论】:

    • 谢谢!我在发布之前研究这个问题时遇到了这个问题,但无法让它工作。没有正确设置 DependencyProperty,所​​以我需要阅读更多内容!最后一件事,什么是检测何时设置 DependencyProperty 的好方法?所以我可以更新按钮等的状态。实现 INotifyPropertyChanged?再次感谢!
    • 啊,没关系,UserControl 的加载事件工作得很好。按钮现在按预期工作!现在我只需要解决音量滑块的绑定,因为它们不是 UserControl 的一部分... DataGridTextColumn 可以很好地检索名称 - 不确定为什么 TemplateColumns 无法获取音量属性。上下文相关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2010-09-09
    相关资源
    最近更新 更多