简体   繁体   中英

Set Property of UserControl in DataGrid?

I'm working on a program for playing audio files and have ran into some issues with one of my DataGrids. Basically it's meant to allow the user change settings for it (Listen, repeat and volumes for example) .

For this I have made my own UserControl, which is being dynamically added via DataTemplates in XAML. So first of all here's some context;

SoundFile.cs (public class)
Class that has file-related properties such as Name, Path, Category and Volumes.

SoundLibrary.cs (public static)
This is a static class which keeps track of all SoundFiles inside an ObservableCollection, this is the collection used to populate my DataGrids.

SoundControl.cs (UserControl)
This is my UserControl which consists of a few buttons, it has a property for SoundFile which is what I'm confused as to how to set and/or properly access.

This is how my DataGrid is defined;

<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>

And here's how I bind it in code behind;

MyDataGrid.ItemsSource = SoundLibrary.Library;

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

Doing that I can access the SoundFile as expected, however I'm not sure how to access the SoundFile through the SoundControl .

The SoundControl class is very simple;

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();
        }
    }
}

Any pointers in the right direction would be much appreciated, still relatively new to data binding and using DataGrids... Thanks in advance!

The Sound property of the SoundControl should be a bindable property, ie dependency property:

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); }
}

You would bind it to the current SoundFile item of the DataGrid row like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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