我有一个包含来自设置对象的信息的 DataGrid。目前,DataGrid 和设置对象之间存在双向绑定。但是,我想在其中放置一个“保存”按钮,如果用户单击“保存”按钮,它只会将 DataGrid 中所做的更改绑定到对象。但是,我不确定如何使用 DataGrid 为我的特定情况调用 UpdateSource()。
这是我的 xaml.cs 代码:
public void LoadDataFields(Data d)
{
Grid1.ItemsSource = d.Fields;
}
private void SaveChanges(object sender, RoutedEventArgs e)
{
BindingExpression be = Grid1.GetBindingExpression(DataGrid.ItemsSourceProperty);
be.UpdateSource();
}
Run Code Online (Sandbox Code Playgroud)
这是我的 xaml 代码:
<DataGrid x:Name="Grid1"
IsReadOnly="False"
Height="360"
Margin="20,15,20,15"
VerticalAlignment="Top"
AutoGenerateColumns="False"
CanUserAddRows="False" SelectionUnit="Cell"
ItemsSource="{Binding data}"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length of Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=length, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以调用 UpdateSource() 以便仅在单击“保存”按钮时才进行绑定?我的猜测是我只是为 …
我正在尝试构建一个新的应用程序,通过TCP/IP与另一个已完成的应用程序通信.这两个应用程序在彼此之间来回传递消息.以下是我的新应用程序中的一些服务器代码,它从已完成的应用程序中读取内容:
public class AsyncService
{
private IPAddress ipAddress;
private int port;
char[] STX = new char[] { '' };
char[] ETX = new char[] { '' };
NetworkStream networkStream;
TcpClient tcpClient;
private byte[] rawBuffer;
private static readonly int RAWBUFFERSIZE = 2024;
public AsyncService(int port)
{
this.port = port;
string hostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
rawBuffer = new byte[RAWBUFFERSIZE];
this.ipAddress = IPAddress.Any;
if (this.ipAddress == null)
{
throw new Exception("No IPv4 address for server");
}
}
public async void …Run Code Online (Sandbox Code Playgroud)