-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make TwoWay binding with templates? #32
Comments
Is the type you are binding to <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Values}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding .}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid> with: public class ViewModel
{
public ObservableCollection<int> Values { get; } = new ObservableCollection<int> {1, 2, 3};
} |
Thanks for answer! Yes, it's about |
What I want: view and edit values in HEX format, in addition I need to set names of rows and columns. Today I'm readed issue "Scientific Notation for Numeric Data", created by kaneorotar, he has a similar problem. Maybe we will try to write a complete example with templates and twoWay binding for other people, who will have this problem in the future? |
Now I'm try do this: (but it's not working)
XAML code:
|
The code was not a suggestion for a fix, it was an example of the limitation you hit. |
And so, should I hope for comlete example, or I must create it by myself? |
I'll ping you if I write one but not sure when I will find the time. |
Ok, anyway, thanks for support)) |
@odinsacred for now you can use converter like this: public class EditableInt2DConverter : IValueConverter
{
class ItemWrapper
{
int[,] container;
int i, j;
public ItemWrapper(int[,] container, int i, int j)
{
this.container = container;
this.i = i;
this.j = j;
}
public int Value
{
get => container[i, j];
set => container[i, j] = value;
}
}
public object Convert(object value, Type targetType, object p, CultureInfo ci)
{
var array2d = (int[,])value;
var n = array2d.GetLength(0);
ItemWrapper[,] items = new ItemWrapper[n, n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
items[i,j] = new ItemWrapper(array2d, i, j);
return items;
}
public object ConvertBack(object value, Type targetType, object p, CultureInfo ci) =>
throw new NotSupportedException();
} dataGrid2D:ItemsSource.Array2D="{Binding Path=Data2D, Converter={StaticResource EditableInt2DConverter}}" so, you will bind to <dataGrid2D:Cell.Template>
<DataTemplate>
<TextBlock IsEnabled="true" Text="{Binding Path=Value, StringFormat=X4}" />
</DataTemplate>
</dataGrid2D:Cell.Template>
<dataGrid2D:Cell.EditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Value, StringFormat=X4}" />
</DataTemplate>
</dataGrid2D:Cell.EditingTemplate> |
@FoggyFinder Thank you very much! |
Hello! Can anybody help me? I wanna bind 2D int array to DataGrid, but I don't understand how to create TwoWay binding. Now I'm use this code:
Also I'm tried to add an attributes like "Mode=TwoWay, BindsDirectlyToSource=True, UpdateSourceTrigger=LostFocus, NotifyOnSourceUpdated=True" In data templates, but it's do nothing effect. Now it's allow me to entering some numbers in cells, but not save to my array. And one detail - everything work good without templates, but I need them.
The text was updated successfully, but these errors were encountered: